?
Solved

PHP Variables and if statements

Posted on 2015-02-21
3
Medium Priority
?
38 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 1000 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 111

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 1000 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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