New terms from treehouse – PHP Basics – What is PHP?, PHP Data & Structure, PHP Datatypes, PHP Operators, and PHP Conditionals and Loops

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!

Screen Shot 2014-12-09 at 9.42.11 AM

The server side process goes like this. Note that php is not interpreted by the browser, but by the server.

Screen Shot 2014-12-09 at 9.43.24 AM


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.

Screen Shot 2014-12-09 at 9.51.02 AM Screen Shot 2014-12-09 at 9.50.53 AM


PHP Data & Structure

PHP Variables

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.

Screen Shot 2014-12-09 at 10.05.17 AM Screen Shot 2014-12-09 at 10.05.20 AM

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.

Screen Shot 2014-12-09 at 10.16.33 AM


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.

Screen Shot 2014-12-09 at 10.22.02 AM Screen Shot 2014-12-09 at 10.21.59 AM

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.


PHP Datatypes

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().

Screen Shot 2014-12-09 at 10.34.51 AM Screen Shot 2014-12-09 at 10.34.48 AM

You can do math with them, obviously.

Screen Shot 2014-12-09 at 10.37.23 AM Screen Shot 2014-12-09 at 10.37.17 AM

Floats let you use decimals.

Screen Shot 2014-12-09 at 10.38.17 AM


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.

Screen Shot 2014-12-09 at 10.54.45 AM Screen Shot 2014-12-09 at 10.54.42 AM

You could change a character using the curly braces and setting it equal to something else.

Screen Shot 2014-12-09 at 10.56.20 AM

To have a second string appear on a new line you’d use an escape sequence by adding a \n to the first line.

Screen Shot 2014-12-09 at 10.59.24 AM Screen Shot 2014-12-09 at 10.59.14 AM

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.

Screen Shot 2014-12-09 at 11.02.44 AM Screen Shot 2014-12-09 at 11.02.41 AM

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.

Screen Shot 2014-12-09 at 11.07.54 AM Screen Shot 2014-12-09 at 11.07.49 AM


PHP Constants

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.

Screen Shot 2014-12-09 at 11.20.09 AM Screen Shot 2014-12-09 at 11.20.20 AM Screen Shot 2014-12-09 at 11.20.25 AM


Arrays

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.

Screen Shot 2014-12-09 at 11.27.04 AM

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.

Screen Shot 2014-12-09 at 11.27.57 AM

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.

Screen Shot 2014-12-09 at 11.30.56 AM

To add values to the end of an array, just leave the square brackets blank.


Associative Arrays

Associate arrays let us use a string as a key. To do this, before each value put a string, then =>.

Screen Shot 2014-12-09 at 11.39.14 AM Screen Shot 2014-12-09 at 11.39.08 AM

Now, it’s easier to use key value pairs, because the keys are contextual.

Screen Shot 2014-12-09 at 11.42.01 AM Screen Shot 2014-12-09 at 11.41.57 AM


PHP Operators

Operators

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 =.

Screen Shot 2014-12-09 at 12.03.44 PM

To autoincrement or decrease, write the variable name followed by ++ or –. So, $a++ would equal 11.


Comparison Operators

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.

Screen Shot 2014-12-09 at 12.12.00 PM Screen Shot 2014-12-09 at 12.12.28 PM


Logical Operators

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.

Screen Shot 2014-12-09 at 12.17.54 PM Screen Shot 2014-12-09 at 12.18.17 PM


PHP Conditionals and Loops

Conditionals

Pretty much the same here as everywhere else.

Screen Shot 2014-12-10 at 2.59.39 PMScreen Shot 2014-12-10 at 3.00.23 PM

Here’s are some simple if block and if/block examples.

Screen Shot 2014-12-10 at 3.06.28 PMScreen Shot 2014-12-10 at 3.06.33 PM

Screen Shot 2014-12-10 at 3.08.05 PM Screen Shot 2014-12-10 at 3.08.11 PM


If / ElseIf Conditionals

ElseIf let’s you add an additional condition.

Screen Shot 2014-12-10 at 3.13.33 PM Screen Shot 2014-12-10 at 3.12.12 PM


For Loops

Screen Shot 2014-12-10 at 3.17.30 PM

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.

Screen Shot 2014-12-10 at 3.21.50 PM Screen Shot 2014-12-10 at 3.21.44 PM


Foreach Loops

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.

Screen Shot 2014-12-10 at 3.28.17 PMScreen Shot 2014-12-10 at 3.30.28 PM Screen Shot 2014-12-10 at 3.30.20 PM

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s