Jump to content

How to echo inside an echo?


brucegregory

Recommended Posts

So I need to echo a row from my database with php, but where i need to echo is already inside an echo. This is my part of my code:

 

 

$con = mysql_connect("$host","$username","$password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("main", $con);

 

$result = mysql_query("SELECT * FROM Vendor");

 

while($row = mysql_fetch_array($result))

  {

//I need to echo right here  .................. but I get a blank page when I try this. Please Help.

  echo '<option value=$row['vendor_id']>'; echo $row['vendor_id']; echo '</option>';

  }

 

mysql_close($con);

 

Result: A Blank page.

Thanks in advance!

Link to comment
Share on other sites

From what I am seeing, the easiest way to do this is to use double quotes, since it will allow you to echo your php variables.

Also, I would suggest that you store the variables before your echo statement

 

Try this:

 

while($row = mysql_fetch_array($result))
  {
$vendorId = $row['vendor_id'];
echo "<option value='$vendorId'>"  . $vendorId. '</option>';
}

 

Link to comment
Share on other sites

From what I am seeing, the easiest way to do this is to use double quotes, since it will allow you to echo your php variables.

Also, I would suggest that you store the variables before your echo statement

 

Try this:

 

while($row = mysql_fetch_array($result))
  {
$vendorId = $row['vendor_id'];
echo "<option value='$vendorId'>"  . $vendorId. '</option>';
}

 

 

Why would you suggest that? They are stored - in an array, creating extra variables for no reason is just a waste of server resources.

 

 


$con = mysql_connect($host, $username, $password); // no need to quote your variables here
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


mysql_select_db('main', $con); // single quotes slightly faster due to not checking for variable interpolation


$result = mysql_query("SELECT * FROM Vendor");


while($row = mysql_fetch_array($result))
  {
//I need to echo right here  .................. but I get a blank page when I try this. Please Help.
  // echo '<option value="'. (int)$row['vendor_id'] .'">'. (int)$row['vendor_id'] .'</option>'; // if vendor ID is integer
  echo '<option value="'. htmlentities($row['vendor_id'], ENT_QUOTES, 'UTF-8') .'">'. htmlentities($row['vendor_id'], ENT_NOQUOTES, 'UTF-8') .'</option>'; // if vendor ID is string
  
  }
mysql_close($con);

Link to comment
Share on other sites

The reason I suggested that is for re usability. He is echoing the variable twice, and in my personal taste, it is easier to define the variables that way any changes in the future are easy to made. As far as wasting processing power I did want to ask what is the difference between storing the variable  and calling the htmlentities function twice for every echo? 

Link to comment
Share on other sites

using double quotes is also a waste of server resources as it then has to check every string for embedded variables, using single quotes and concatenation is a better way of doing it, because it does not check strings with single quotes for embedded variables.

Link to comment
Share on other sites

using double quotes is also a waste of server resources as it then has to check every string for embedded variables, using single quotes and concatenation is a better way of doing it, because it does not check strings with single quotes for embedded variables.

 

The difference in performance is negligible, though you are correct. If changes like these actually affect the way your script runs, you might want to look at moving away from PHP to a language designed for speed, or throw more hardware at it :)

Link to comment
Share on other sites

using double quotes is also a waste of server resources as it then has to check every string for embedded variables, using single quotes and concatenation is a better way of doing it, because it does not check strings with single quotes for embedded variables.

 

The difference in performance is negligible, though you are correct. If changes like these actually affect the way your script runs, you might want to look at moving away from PHP to a language designed for speed :)

 

Or buy another stick of RAM and forget about it.

 

Shaving nanoseconds off script execution is a waste of effort.

Link to comment
Share on other sites

PHP5+ is optimized for interpolated strings, believe it or not.  Check this speed test:

<?php

$short = array("apple", "banana", "coconut");
$long = array("Alice", "Bob", "Claudia", "Dan", "Edgar", "Frank", "George", "Harry", "Ignacious", "Jerry", "Kate", "Larry");

echo "Interpolation...\n";

$start = microtime(true);
for ( $p = 0; $p < 1000000; $p++ ) {
$a = "My three favorite foods are: {$short[0]}, {$short[1]}, and {$short[2]}";
}
echo "\tShort: " . number_format(microtime(true)-$start,2) . " seconds.\n";

$start = microtime(true);
for ( $p = 0; $p < 1000000; $p++ ) {
$a = "There's a lot of people here, like {$long[0]}, {$long[1]}, {$long[2]}, {$long[3]}, {$long[4]}, {$long[5]}, {$long[6]}, {$long[7]}, {$long[8]}, {$long[9]}, {$long[10]}, and {$long[11]}";
}
echo "\tLong: " . number_format(microtime(true)-$start,2) . " seconds.\n";



echo "\nConcatenation...\n";

$start = microtime(true);
for ( $p = 0; $p < 1000000; $p++ ) {
$a = 'My three favorite foods are: ' . $short[0] . ', ' . $short[1] . ', and ' . $short[2];
}
echo "\tShort: " . number_format(microtime(true)-$start,2) . " seconds.\n";

$start = microtime(true);
for ( $p = 0; $p < 1000000; $p++ ) {
$a = 'There\'s a lot of people here, like ' . $long[0] . ', ' . $long[1] . ', ' . $long[2] . ', ' . $long[3] . ', ' . $long[4] . ', ' . $long[5] . ', ' . $long[6] . ', ' . $long[7] . ', ' . $long[8] . ', ' . $long[9] . ', ' . $long[10] . ', and ' . $long[11];
}
echo "\tLong: " . number_format(microtime(true)-$start,2) . " seconds.\n";


echo "\nBare strings, no variables...\n";

$start = microtime(true);
for ( $p = 0; $p < 1000000; $p++ ) {
$a = 'My three favorite foods are: apples, bananas, and coconuts';
}
echo "\tSingle quoted string: " . number_format(microtime(true)-$start,2) . " seconds.\n";

$start = microtime(true);
for ( $p = 0; $p < 1000000; $p++ ) {
$a = "There's a lot of people here, like Alice, Bob, Claudia, Dan, Edgar, Frank, George, Harry, Ignacious, Jerry, Kate, and Larry";
}
echo "\tDouble quoted string: " . number_format(microtime(true)-$start,2) . " seconds.\n";


echo "\nDone!\n";

 

Outputs:

 

[maniacdan@maniacdan ~]$ php bar.php
Interpolation...
        Short: 0.45 seconds.
        Long: 1.59 seconds.

Concatenation...
        Short: 0.53 seconds.
        Long: 2.38 seconds.

Bare strings, no variables...
        Single quoted string: 0.14 seconds.
        Double quoted string: 0.15 seconds.

Done!

Concatenation is slower, especially the more variables you're concatenating.  Interpolation is faster.  It doesn't make any sense, but there it is.

 

For bare strings without ANY variables, double-quotes are only very very slightly slower.  Nothing compared to the benefit of using them when there's variables involved.

 

(Test run on 5.3.8)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.