Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

PHP Variables and if statements

How do I create a variable, that includes and if statement.
<?php
function myFunction() {
$html = '';
  $query = SELECT name FROM Table_Name;
  $data = $data->result_array();
  foreach ($data as $row) {
  $name = $row['name'];
  $html. = '<div class="my-class">Hello:: '.   if($name !="") {echo $name;} else {echo "Stranger";}  .' How are you today?<br /></div>';
  }
return $html;
}

Open in new window


How do I write this part::   if($name !="") {echo $name;} else {echo "Stranger";}
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dave's giving you the right advice here.  Create your variables separately, then test them to see if they are usable, then render the variables in the output strings.  Something like this.  The design pattern plays well with HEREDOC notation.

<?php // demo/temp_rgranlund.php
/**
 * See: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28621903.html
 */
error_reporting(E_ALL);

// SET AN ORIGINAL VALUE FOR A DATA ELEMENT
$thing = NULL;

// TEST THE ORIGINAL VALUE AND PROVIDE A SUBSTITUTE IF THE ORIGINAL IS USELESS
$thing
= (!empty($thing))
? strtoupper($thing)
: 'NO USABLE THING'
;

// SHOW THE FINAL VALUE FOR THE DATA ELEMENT
echo $thing;

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial