Share
Embed
Transcript
42 slides
-
PHP: Hypertext Preprocessor PHP is a server scripting language, and
a powerful tool for making dynamic and interactive Web pages. - W3Schools -
BACK END PHP and similar languages are often referred to
as the “back end” or “server” side of websites. Servers use these languages to serve HTML documents that browsers request. Building HTML on the server allows for content to be populated dynamically and removes the need to maintain a separate .html file for every page. -
WHAT’S IT GOOD FOR? Imagine you have 1,000 pages on
your website and all of them are built out as separate .html files. Now imagine that you need to change the footer on every page. If the pages were constructed dynamically on the server you could change the .php file containing your site’s footer once and be done with it. -
WHO USES IT? Wikipedia, parts of Facebook, parts of Yahoo,
WordPress, Vimeo, MailChimp, Drupal, and a large portion of the Internet. -
WHAT ELSE IS OUT THERE? Other server-side technologies include: ASP,
Ruby, Python, Node.js, Perl There are a lot of options out there and each has its pros and cons. But, PHP is one of the most commonly used server-side languages and almost any web hosting company will come with PHP installed. -
CAN I USE IT ON MY COMPUTER? Yes! Unfortunately the
setup can vary greatly depending on your operating system and what kind of server configuration you’d like. We won’t go over setting it up in this workshop, but you can find numerous tutorials on the web that should get you set up. -
MYSQL MySQL is a type of database that is commonly
used with PHP. A database lets you store and retrieve data for use in your website. Pretty much any site you use has some sort of database component. We don’t have time to get into it today, but you’ll want to make sure you learn the basics. -
HOW DOES IT WORK? 1. User requests a page from
website 2. Server routes request to appropriate .php file 3. .php file connects to database to get content 4. PHP scripts format content into HTML document 5. Browser loads HTML document 6. Browser downloads CSS and JavaScript files 7. User interacts with page -
LET’S START WITH THE FUNDAMENTALS Programming languages have variables which
can contain values. Examples: $testString = “Testing”; $testNumber = 42; $testBoolean = true; -
Variables allow you to store data so that it can
be referred to later. -
LET’S SEE SOME EXAMPLES Head over to http://phpepl.cloudcontrolled.com/ so we
can play around with some PHP -
This part of the presentation is being live coded with
the class. Sorry. -
MATH We’ll often need to perform mathematical operations in our
code. Basic math can be performed with different operators. Operators are characters that PHP recognizes as performing a certain function. -
Addition: + Subtraction: - Mutliplication: * Division: / Remainder Division
(Modulus): % Exponents: ^ Increment: ++ Decrement: - - ARITHMETIC OPERATORS -
LET’S DO SOME MATH Head back to http://phpepl.cloudcontrolled.com/ so we
can perform some quick math -
This part of the presentation is being live coded with
the class. Sorry. -
ARRAYS Arrays are collections of data where each item in
the collection is assigned a numerical index. These indexes always start at 0. You can think of them like a list of items. To access a single item in the array you use the variable name of the array and the index of the item you want to access. -
$fruitArray = [ ‘banana’, //0 ‘apple’, //1 ‘orange’ //2 ];
echo $fruitArray[1]; //this will output “apple” -
$numberArray = [ 12345, //0 67890, //1 14323, //2 78519
//3 ]; echo $numberArray[0]; //this will output 12345 -
ASSOCIATIVE ARRAYS Arrays in PHP can also have named indexes
instead of numeric indexes. These arrays are called “associative” arrays. -
$fruitArray = [ ‘banana’ => ‘yellow’, //$fruitArray[‘banana’] ‘apple’ => ‘red’,
//$fruitArray[‘apple’] ‘orange’ => ‘orange’ //$fruitArray[‘orange’] ]; echo $fruitArray[‘banana’]; //this will output “yellow” -
IF STATEMENTS If statements are one of the basic building
blocks of programming logic. You tell the interpreter to execute the code inside the statement if a certain condition is met. -
Equality: === Inequality: !== Less Than: < Less Than or
Equal To: <= Greater Than: > Greater Than or Equal To: >= AND: && OR: || COMPARISON OPERATORS -
$fruit = “banana”; if($fruit === “apple”) { echo “Apples are
awesome.”; } else { echo “Boo! This is a “ . $fruit . “.”; } -
LOOPS Loops allow you to iterate through an array or
to execute some code a specific number of times. We will be talking about the for loop and the while loop. -
FOR is great for iterating through arrays WHILE is great
for running code until a condition is met -
$fruitArray = [ ‘apple’, ‘banana’, ‘orange’ ]; for($i = 0;
$i < count($fruitArray); $i++) { echo “Current fruit: “ . $fruitArray[$i]; } -
$fruitArray = [ ‘apple’, ‘banana’, ‘orange’ ]; $notOrange = true;
$count = 0; while($notOrange) { if($fruitArray[$count] === ‘orange’) { $notOrange = false; } echo $fruitArray[$count] . “is not an orange.”; $count++; } -
FUNCTIONS Functions are reusable blocks of code that perform an
action. You can create a function out of any parts of your code. You call a function by using it’s name and then a set of parentheses ( ). -
function fruitFunction() { $fruitArray = [ ‘apple’, ‘banana’, ‘orange’ ];
for($i = 0; $i < count($fruitArray); $i++) { echo ‘Current fruit: ‘ . $fruitArray[$i]; } } fruitFunction(); //this triggers our code above -
OUTPUT echo outputs text to the screen print_r outputs structure
and data of array var_dump outputs type and value include adds code from .php file -
Head back to http://phpepl.cloudcontrolled.com/ so we can write some PHP.
There are far too many useful PHP methods for us to explore even a fraction of them, so we’ll only work with a few of them. You can find information about every PHP method at http:// php.net/ -
This part of the presentation is being live coded with
the class. Sorry.