What is PHP?
PHP is a server-side web language, standing for PHP hypertext pre-processor. It’s html centric, meaning you can embed it directly into your html with code blocks. As a scripting language you can write and run scripts by adding .php to the end of a file and call it using the php CLI (command line interpreter). It’s a server-side language, meaning it uses embedded scripts in html, which make requests from the client side machine to the host site, which processes them and responds in html.
To open php code in html, use the <?php> tag. Close statements with semicolons. In the example below, it would return Hello World!
The server side process goes like this. Note that php is not interpreted by the browser, but by the server.
PHP Basic Usage
The current state as of this video is 5.5. Start by renaming your file to index.php, In the example below we put some php inside a p element. So, when the php was interpreted by the server, we basically said we wanted it to return it as html.
PHP Data & Structure
PHP variables always begin with a dollar sign, followed by a letter or underscore (not a number), then followed by a combination of underscores, letters or numbers. Note that the variable name is case sensitive. We’re going to add some variables at the top of the document, before the DOCTYPE html tag. Note that on a single line of php, you don’t actually need to end it with a semicolon.
Like most languages, in php, the order of operation goes from top to bottom,
PHP Statements and Comments
“I’m not commenting to remember it later, I’m commenting to remember it now”. Two // in front of a line will comment it out. To do a multiline comment, do it like CSS with /* */.
A statement in php is anything that’s an executable line, which are ended with semicolons.
Whitespace in PHP
Whitespace doesn’t matter in php. In the example above, it’s standard practice to write variable name, space, equals, space and then the string. But, you could remove the quotes for the same result. You can wrap your php in the html in a pre element to have them display better. Here we have an array.
If we were to remove all the whitespace, or add a bunch more, the result would be the same. The reason we include it is for readability.
Integers and Floats
There are 7 data types in php: integers, floats, strings, arrays, boolean, objects, and resources. Floats hold fractal numbers, ie ones with decimals. Resources are anything that’s not php data, like files or responses.
Integers are whole numbers. To set a variable to one, don’t use quotes, as that would make it a string. To know whether something is a string or integer, there are built in functions in PHP, in this case gettype().
You can do math with them, obviously.
Floats let you use decimals.
Strings and Booleans
Strings are any collection of characters. You can pull out individual characters using the index of the string, where 0 is the first character, 1 the second, etc.
You could change a character using the curly braces and setting it equal to something else.
To have a second string appear on a new line you’d use an escape sequence by adding a \n to the first line.
Booleans are just true or false, so set it to TRUE or FALSE. You can use var_dump() to have it print out the variable and its value.
Anything that’s a non-0 value is true, so a number over 0, a non empty string, etc. We can use type casting to test, by putting (bool) inside the var_dump function. Here we test an empty string, 0, a float, and an empty array.
isset() is a function that you can pass a variable to see if a variable is set (ie has a value) and not null. If it is, it will return the value TRUE.
Constants are different than variables in that they display values that don’t change. To define one, you use the internal php function define(), which you would typically do at the top of the page. You put the name in quotes, then a comma, then the value. The name doesn’t have to be in all caps, but this is usually done to denote a constant vs a variable. You can’t start the name with a number. To print, simply write echo and the constant name.
To define an empty array just set a variable equal to array(). You can also use shorthand notation and not have to write array with square brackets.
Arrays can contain any kind of data, just put them in a commas separated list, with each value in single quotes. If you print it using the print_r() function it will lis tthe index of each value. The index is the key, the value for it is what we set, which means that an array is a series of key value pairs.
You can modify/recall different values using their key. Do that by putting a square brackets after the array’s name, with the index for the value you want.
To add values to the end of an array, just leave the square brackets blank.
Associate arrays let us use a string as a key. To do this, before each value put a string, then =>.
Now, it’s easier to use key value pairs, because the keys are contextual.
Operators are symbols that let us modify values. Unary involve one operator, and would be like + or -. Binary have two, and would like >= or <=. Ternary have three, like ===. Comparison compare two values, like === or !=. Concatenation ones like you combine values, like a dot. Arithmetic are for math like + or -. Autoincrements like ++. Assignment operators are like =.
To autoincrement or decrease, write the variable name followed by ++ or –. So, $a++ would equal 11.
These let you compare two values. == is for if things are equal in value, but === also checks if they’re equal in type, like integer vs string. != is for not equal to. !== also checks type. We can use the var_dump function here to help us compare and see if it’s true or false. <, >, >= and <= are other options as well.
These take two values and compare them logically. and(also written as &&) is one, and with var_dump we’re checking if both values are true, which they both need to be for it to return true. or(also written as ||) returns true if either value is true. not(represented by a !) returns true so long as the value is not true.
PHP Conditionals and Loops
Pretty much the same here as everywhere else.
Here’s are some simple if block and if/block examples.
If / ElseIf Conditionals
ElseIf let’s you add an additional condition.
Here we’ll do a basic for loop. First you set the variable, then the condition that the loop will continue to run so long as its true, then an increment increase on the variable.
Here we’ll add additional social media links via a foreach loop. We’ll start by making an array of class names. We need to loop through this and change the class name for our list items. Notice how we were able to just put the php where we need it, and that the foreach loops code to run actually encompassed the original html. The as keyword makes it so each item in the social icon array is set to the variable $icon.