Basics
<?php
?>
Placeholders for values that we don't know, or that may change.
<?php
$my_var;
?>
<?php
// string
$first_name = "Phil";
// number
$total = 99;
// numbers can include decimals, no commas
$total = 99.15
?>
<body>
<?php
$first_name = "Phil";
?>
...
<body>
<?php
$first_name = "Phil";
echo $first_name;
?>
...
<body>
<?php
$first_name = "Phil";
echo $first_name;
print $first_name;
?>
<body>
<?php
$first_name = "Phil";
$total = 99;
print $first_name;
echo '
';
echo $first_name, $total;
?>
// Single line comment
# Single line comment
/*
Multiple lines in this comment.
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Possimus, assumenda, rem excepturi perferendis dicta vel pariatur!
*/
$school = 'Drexel';
$mascot = 'Dragons';
echo $school . ' ' . $mascot;
echo 'My favorite college team is the ' . $school . ' ' . $mascot;
+ - * /
$my_number = 5;
echo $my_number + 2; // 7
$number = 5;
$result = ++$number * 2; // 12
echo 'number = ' . $number . ' | ' . 'result = ' . $result;
// output: number = 6 | result = 12
$number = 5;
$result = $number++ * 2; // 10
echo 'number = ' . $number . ' | ' . 'result = ' . $result;
// output: number = 6 | result = 10
Lists of values
<?php
// The old way
$characters = array('one','two','three');
// The new way
$characters = ['Bugs','Daffy','Speedy'];
<?php
$characters = ['Bugs','Daffy','Speedy'];
print_r($characters);
// output: Array ( [0] => Bugs [1] => Daffy [2] => Speedy )
<?php
$characters = ['Bugs','Daffy','Speedy'];
echo $characters[0]; // Bugs