Jump to content

[NEWBIE ALERT] Differences between these echo statements...

- - - - -

  • Please log in to reply
9 replies to this topic

#1
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi all,

As you can probably ascertain from my profile and avatar - I am a PHP newbie and will probably flood this board with my work and where I need clarification. I'm from Toronto (Go Leafs Go!) and am open to any advice on materials, courses, tutorials, or tutors on PHP/MySQL.

The first thing I need clarification on is as follows:

<?php


echo "Hello World!" . "<br />";

echo('Hello World!' . '<br />');

echo("Hello World!" . '<br />');


?>

I'm confused as to the differences in these three statements. #1 comes from w3schools, #2 comes from a PHP coding friend, and #3 is my own mix of the preceeding two to see what would happen. They all output the same result.

I'm wondering if these comparisons (and learning the proper form for it) will eventually catch up to me later down the line and impede my learning/coding ability.

Thanks again for your help, and I look forward to learning from all of you and making some friends along the way!

Regards,

#2
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Took it a step futher:

<?php

echo('<b>' . 'Example Set 1' . '</b><br />');

echo "Hello World!" . "<br />";

echo('Hello World!' . '<br />');

echo("Hello World!" . '<br />');


// Testing the differences between the aformentioned echo statements


echo('<b>' . 'Example Set 2' . '</b><br />');

echo "Hello World!" . "<br />";

echo('Hello World!') . '<br />';

echo("Hello World!") . '<br />';

?>

Is there really any efficiency, syntax, or other purpose for doing it any of these ways? Is any of this bad form, bad efficiency, whatever? They all seem to output the same thing.

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
many people skip the parenthesis on the echo, as it's cleaner to read. it's more of a matter of taste.
the difference between surrounding a string with " or ' is small, but important. strings within " are parsed if there are any variables to be replaced with it's value, while ' is not parsed. on the other hand, you need to escape a ' if the string is surrounded with ', and vice verca.
in this case, I personally would use:
echo 'Hello World!'.'<br />';
as there is nothing to parse in the strings. On the contrary, I don't like the string parsing at all, I always use the concatenation (period) to fill in variables, to a) make sure it's not wrongly parsed b) to have the variable easier seen in my syntax highlighter in the editor. But this is my personal feeling.

doing
$var=5;

echo "Variable var is $var";

outputs the same as
$var=5;

echo "Variable var is ".$var;

but you can read the later part better with syntax highlighting.

when writing sql queries, I always use " instead of ', as you need to use the ' more in the syntax, on the other hand, I use ' around html output, as you use so many " around the attribute values
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
An authoritative source should help as well:
PHP: Strings - Manual

echo is a language construct (think like print in other language, but enhanced) to help you write multiple strings in different ways, they are all equally efficient from a web standpoint.

Also remember escapes are generally only done in double quotes, for a newline it is "\n" for example (handy when writing text to a text file)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Sorry - can you explain what an 'escape' is? Thanks for the definitive definition for strings, I will go through it now!

Cheers

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
An escape is an alternate way to display a character, for example "\t" is tab and "\n" is newline character, for example if you wished to write a neat formatted table (quick example):

echo "<pre>1\t2\t3 \n4\t\5\t\6 \nI am on the third line</pre>"
Will print this in the browser:

1    2    3
4    5    6
I am on the third line

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Two issues:

1 - <pre> and </pre> tabs?
2 - I tried replacing <br /> with \n and it wouldn't imitate. Some clarification? Might be related to <pre>?

#8
PHP4ME

PHP4ME

    Newbie

  • Members
  • PipPip
  • 12 posts
Disregard - <pre> tags solved in my HTML reference book. It preserves the line and width of the given values/text. Still unsure why \n wouldn't work though to replace my <br> tags.

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Often when you are working with logs (and HTML forms), you need to atleast understand plaintext formatting (the stuff I mentioned) as that can help you understand how the data input/output/sensitization works.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
About escaping - if you want to output a quote sign and it's surrounded by quotes, you need to ad an backslash before the quote to be outputted, telling "this is not the end of the string, it's an output character!"
echo "I'm echoing an quote(\") here!";

echo 'I\'m echoing an quote (") here!';


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users