Solved

PHP Variables and if statements

Posted on 2015-02-21
3
32 Views
Last Modified: 2015-03-07
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";}
0
Comment
Question by:rgranlund
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 40623699
You don't.  You can't put an 'if' statement like that as part of a string.  It needs to go on it's own line.
  $html. = '<div class="my-class">Hello:: ':
if($name !="") {echo $name;} else {echo "Stranger";};  
echo ' How are you today?<br /></div>';

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40623749
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

0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 40624063
An alternative approach is to use what's called a ternary operator, along with sprintf(). Also avoids the mess that can can be string concatenation:

$html .= sprintf('<div class="my-class">Hello:: %s. How are you today?</div>', ($name != "") ? $name : "Stranger");

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now