PHP print text
All php code starts with the following code
<?php and always ends with
While you can use shorter versions, for example:
<? ... ?> it’s more consistent and safer to use the full php tags.
As you’ll have noticed with our first page using phpinfo(), the line of code is terminated with a semi colon. These are used to terminate every line of code besides control structures (more on these later). PHP and HTML code can be mixed in one page, however they cannot cross each other, ie. all php must be enclosed with the php tags, and if HTML is to be used within PHP then it must be presented in a particular way (see later).
So a simple page to print the typical ‘Hello World’ statement
<html> Another word which does the same as “print” is “echo”, however this tends to cause some syntax problems at times and seeing as there is no difference I always use print.
<title>Hello World Practice</title>
<?php print "Hello World"; ?>
When using print, all strings are enclosed with double quotes. Single quotes can be used but a more consistent approach is double quotes. If you want to display a double quote within a print statement then you just escape it with a backslash eg.
<?php print "He said \"Where are you going?\", and then walked away"; ?>
When this is interpreted, the backslash tells the interpreter to treat the double quote as a string and not a double quote. The following would not work:
<?php print "He said "Where are you going?", and then walked away"; ?>
This would give a syntax error. However single quotes within double quotes are allowed in this situation so:
<?php print "He said 'Where are you going?', and then walked away"; ?> would work fine.