The PHP Practitioner
- https://laracasts.com/series/php-for-beginners
- start a server with
php -S localhost:8888
Step 3: Variables
- commands operations end with
;
- variables start with
$
$var
- period
.
is used for concatenationecho 'Hello ,' . $var;
- double quotes for printing the variable in a string
echo "Hello , $var";
- curly braces can be used for better readability
echo "Hello, {$var}";
Step 4: PHP and HTML
- a file with 100% php has no php closing tag
?>
$_GET['var']
gets the value of a key value pair in the url- with url
http://localhost:8888?var=foo
,$_GET['var']
will returnfoo
- with url
htmlspecialchars()
; Convert special characters to HTML entities- useful for making sure no html is parsed in
$_GET
, aka sanitize
- useful for making sure no html is parsed in
- use
=
as a shorthand for - end css statements with
;
- comment out html with
Step 5: Separate Logic From Presentation
require
loads an external php file- allows separation of logic and rendering
Step 6: Understanding Arrays
-
$myArray[]
defines an empty array -
assign values
$myArray=[ 'item1', 'item2' ]
-
loop over array
foreach($myArray, as $item){ // do stuff } ?>
- or
foreach($myArray,as $item) : ?> // do stuff endforeach; ?>
Step 7: Associative Arrays
- key value pairs use arrow
=>
$myArray['key'=>'value]
- add key/value to existing array
$myArray['newKey']='newValue'
- add value to non associative array
myArray[]='newValue'
- remove key/value
unset(myArray['key'])
- dump variable info with
var_dump($myVar)
die('message')
stops execution- e.g.
die(var_dump($myVar))
- e.g.
Step 8: Booleans
- boolean values
true
/false
- e.g.
$completed = true;
- e.g.
- ternary operator
$condition ? 'do this when true' : 'do this when false';
ucwords('uppercase the first character of each word in a string')