...

View Full Version : adding whitespace to echo problem.



thesavior
01-03-2006, 07:03 AM
adding spaces to an echo.

This code, which im using works:


<?php // connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT DISTINCT category FROM tutorials";
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_array($result))
{
echo $row['category'];
}
?>

What i would like to do, would look more like this, HOWEVER, it doesn't work. What do i change to make it work.


<?php // connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT DISTINCT category FROM tutorials";
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_array($result))
{
echo $row['category']" | ";
}
?>

marek_mar
01-03-2006, 07:15 AM
You have to use the dot ".". In PHP it joins strings together (concatenation operator).


print $string . ' space ' . $another_string;

BTW as echo is not a function it is better to use "," like this:


echo $string, ' space ', $another_string;

It only works with echo though.

Velox Letum
01-03-2006, 07:15 AM
echo $row['category'] . ' | ';

Is this what you mean? Trying to separate categories with a pipe? If so, there's a better way.


<?php // connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT DISTINCT category FROM tutorials";
$result = mysql_query($query) or die('Error, query failed');
$categories = array()
while ($row = mysql_fetch_array($result))
{
$categories[] = $row['category'];
}
echo implode(' | ', $categories);
?>

GJay
01-03-2006, 09:17 AM
BTW as echo is not a function it is better to use "," like this:

I'm curious as to why it's 'better'?

ralph l mayo
01-03-2006, 10:58 AM
I'm curious as to why it's 'better'?

Yeah, I think this is backwards. Commas are argument delimiters and periods are concatenation operators, so it seems if echo isn't a function it would be more in keeping with its spirit to pass it a big chunk of data that's been stuck together externally rather than a bunch of discrete units.

If you're worried about speed, which you shouldn't be anyway because your system is almost certainly not going to be bottlenecked by 'echo' overhead, someone who benchmarked it in the PHP manual says concatenation actually tests faster.

firepages
01-03-2006, 02:30 PM
<edit>lol got the wrong end of the stick :)</edit>

marek_mar
01-03-2006, 03:44 PM
I'm curious as to why it's 'better'?
Yes It's the other way around. I made sure of it now. I mixed it up becouse I never use echo.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum