Solved

PHP Variables and if statements

Posted on 2015-02-21
3
35 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 109

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to use a switch statement with heredoc 11 33
Need to remove %20 from url in get 17 36
Ajax and PHP 9 29
Add a loading gif while php runs server side 15 31
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

856 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