What would a list of How-To-Techniques be on a WordPress site without a “Hello World!” example in PHP?
We think lacking at best, so let’s play with some simple php code in the examples below.
1 | print "Hello, World!"; |
Alternatively we could write it this way:
1 | echo "Hello, World!"; |
Both Yield Hello, World!
If you don’t understand the significance of the PHP “Hello World!” example, then you must not know the advantages server-side programming languages provide. The use of HTML to print Hello World! is simple, but having it done dynamically on the fly is quite another thing. Server-side programming has changed the face of how the web works, and PHP has truly fueled the open source revolution.
Now we’ll move on to the question, “What is the difference between the PHP print() and echo() language constructs”?
The answer, not much – BUT – echo is faster than print. Print returns an int value and echo returns no value. Example: $value = print "Hello World"; here $value will be set to 1. So print can be used in complex expression where you can’t use echo.
We can however, use echo is some creative short hand statements, like this one.
1 | echo($text == 'hi') ? "Hello World!" : "Goodbye World!" ; |
Suppose the variable $text was set to “hi”, this would print Hello World!. If $text was set to anything else then it will print Goodbye World!.
