Solved

PHP Variables and if statements

Posted on 2015-02-21
3
33 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 83

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 43

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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 count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

25 Experts available now in Live!

Get 1:1 Help Now