PHP Basics
Tags & Comments
<?php // The opening PHP tag
// A single line comment
# Also a single line comment
/* A multi-line comment
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Neque, earum magnam quis quisquam enim ea animi ipsam. Nostrum, officiis et. */
/* A PHP closing tag is required when content other than PHP is included in the code block, but is optional if not. */
?>
// A single line comment
# Also a single line comment
/* A multi-line comment
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Neque, earum magnam quis quisquam enim ea animi ipsam. Nostrum, officiis et. */
/* A PHP closing tag is required when content other than PHP is included in the code block, but is optional if not. */
?>
'echo' keyword, strings and semi-colons
<?php
/*
PHP can be run in a browser using a web server such a Apache.
It can also run in a terminal.
Copy this block of code and paste into a new file 'basics.php'.
Navigate to the directory which contains 'basics.php'.
At the command prompt enter: php basics.php
*/
# The 'echo' keyword tells PHP to output some text
echo "Hello World!\n";
/* PHP stores text in 'strings' enclosed in single or double quotes.
Double quoted strings can hold special characters like '\n' which tells PHP to start a new line. */
echo "I am some text\n";
echo "I am some text on a new line\n";
/* PHP statements are terminated with a semi-colon,
although the PHP closing tag implies a semi-colon.*/
echo "No semi-colon is a no-no!\n";
# Using semi-colons multiple statments can be written on the same line.
echo "PHP"; echo " Apprentice";
# No closing tag required in this case but included in this case.
?>
/*
PHP can be run in a browser using a web server such a Apache.
It can also run in a terminal.
Copy this block of code and paste into a new file 'basics.php'.
Navigate to the directory which contains 'basics.php'.
At the command prompt enter: php basics.php
*/
# The 'echo' keyword tells PHP to output some text
echo "Hello World!\n";
/* PHP stores text in 'strings' enclosed in single or double quotes.
Double quoted strings can hold special characters like '\n' which tells PHP to start a new line. */
echo "I am some text\n";
echo "I am some text on a new line\n";
/* PHP statements are terminated with a semi-colon,
although the PHP closing tag implies a semi-colon.*/
echo "No semi-colon is a no-no!\n";
# Using semi-colons multiple statments can be written on the same line.
echo "PHP"; echo " Apprentice";
# No closing tag required in this case but included in this case.
?>
Terminal Output
Hello World!
I am some text
I am some text on a new line
No semi-colon is a no-no!
PHP Apprentice
I am some text
I am some text on a new line
No semi-colon is a no-no!
PHP Apprentice
Browser Ouput
Hello World!
I am some text
I am some text on a new line
No semi-colon is a no-no!
PHP Apprentice
Differences between terminal and browser output
<?php
/*
The browser output doesn't display as expected.
The new line special character '\n' has no effect in the browser.
Instead we can use '<br>' and rewrite the code as follows:
*/
echo "Hello World!<br>";
echo "I am some text<br>";
echo "I am some text on a new line<br>";
echo "No semi-colon is a no-no!<br>";
echo "PHP"; echo " Apprentice";
?>
/*
The browser output doesn't display as expected.
The new line special character '\n' has no effect in the browser.
Instead we can use '<br>' and rewrite the code as follows:
*/
echo "Hello World!<br>";
echo "I am some text<br>";
echo "I am some text on a new line<br>";
echo "No semi-colon is a no-no!<br>";
echo "PHP"; echo " Apprentice";
?>
Browser Output
Hello World!
I am some text
I am some text on a new line
No semi-colon is a no-no!
PHP Apprentice
I am some text
I am some text on a new line
No semi-colon is a no-no!
PHP Apprentice