Brad Traversy PHP Crash Course
Get the source files from here.
Comments
// This is a single line comment.
/* This is a multiple line comment
which might be used to exclude blocks
of code during development. */
/*
This multiple line comment looks
a litte neater and works in the
same way as the above.
*/
/* This is a multiple line comment
which might be used to exclude blocks
of code during development. */
/*
This multiple line comment looks
a litte neater and works in the
same way as the above.
*/
Tags
<?php // This is a php tag. If there is no html or other content below the php, we don't need to close the php tag.
Outputting Content
<?php
// echo :- Output strings, numbers, html, etc
echo 'Hello'; // Lines must be terminated with a semicolon!
echo 123;
echo '<h1>Hello</h1>';
// Closing tag is required if anything other than php code follows.
?>
// echo :- Output strings, numbers, html, etc
echo 'Hello'; // Lines must be terminated with a semicolon!
echo 123;
echo '<h1>Hello</h1>';
// Closing tag is required if anything other than php code follows.
?>
Result
Hello123
Hello
<?php
// print :- Similar to echo, but a bit slower.
print 'Hello';
?>
// print :- Similar to echo, but a bit slower.
print 'Hello';
?>
Result
Hello
<?php
// print_r :- Gives a bit more info. Can be used to print arrays.
print_r('Hello');
echo '<br>';
print_r(['a', 'b', 'c']);
?>
// print_r :- Gives a bit more info. Can be used to print arrays.
print_r('Hello');
echo '<br>';
print_r(['a', 'b', 'c']);
?>
Result
Hello
Array ( [0] => a [1] => b [2] => c )
Array ( [0] => a [1] => b [2] => c )
<?php
// var_dump :- Even more info like data type and length.
var_dump('Hello');
echo '<br>';
var_dump([4, 5, 6]);
?>
// var_dump :- Even more info like data type and length.
var_dump('Hello');
echo '<br>';
var_dump([4, 5, 6]);
?>
Result
string(5) "Hello"
array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) }
array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) }
<?php
// Escaping characters with a backslash.
echo 'Is your name O\'reilly?';
?>
// Escaping characters with a backslash.
echo 'Is your name O\'reilly?';
?>
Result
Is your name O'reilly?
<!-- You can output PHP including variables from within HTML -->
<h1>Hello <?php echo 'Brad' ?></h1> <!-- The semicolon isn't required if the statement is followed immediately by a closing php tag. -->
<h1>Hello <?php echo 'Brad' ?></h1> <!-- The semicolon isn't required if the statement is followed immediately by a closing php tag. -->