Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Geek Girl Tech Con Charlotte - PHP 101

Eric Allen
November 15, 2014

Geek Girl Tech Con Charlotte - PHP 101

Eric Allen

November 15, 2014
Tweet

More Decks by Eric Allen

Other Decks in Education

Transcript

  1. PHP: Hypertext Preprocessor PHP is a server scripting language, and

    a powerful tool for making dynamic and interactive Web pages. - W3Schools
  2. 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.
  3. 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.
  4. WHO USES IT? Wikipedia, parts of Facebook, parts of Yahoo,

    WordPress, Vimeo, MailChimp, Drupal, and a large portion of the Internet.
  5. 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.
  6. 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.
  7. 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.
  8. 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
  9. LET’S START WITH THE FUNDAMENTALS Programming languages have variables which

    can contain values. Examples: $testString = “Testing”; $testNumber = 42; $testBoolean = true;
  10. 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.
  11. Addition: + Subtraction: - Mutliplication: * Division: / Remainder Division

    (Modulus): % Exponents: ^ Increment: ++ Decrement: - - ARITHMETIC OPERATORS
  12. 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.
  13. $fruitArray = [ ‘banana’, //0 ‘apple’, //1 ‘orange’ //2 ];

    echo $fruitArray[1]; //this will output “apple”
  14. $numberArray = [ 12345, //0 67890, //1 14323, //2 78519

    //3 ]; echo $numberArray[0]; //this will output 12345
  15. ASSOCIATIVE ARRAYS Arrays in PHP can also have named indexes

    instead of numeric indexes. These arrays are called “associative” arrays.
  16. $fruitArray = [ ‘banana’ => ‘yellow’, //$fruitArray[‘banana’] ‘apple’ => ‘red’,

    //$fruitArray[‘apple’] ‘orange’ => ‘orange’ //$fruitArray[‘orange’] ]; echo $fruitArray[‘banana’]; //this will output “yellow”
  17. 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.
  18. Equality: === Inequality: !== Less Than: < Less Than or

    Equal To: <= Greater Than: > Greater Than or Equal To: >= AND: && OR: || COMPARISON OPERATORS
  19. $fruit = “banana”; if($fruit === “apple”) { echo “Apples are

    awesome.”; } else { echo “Boo! This is a “ . $fruit . “.”; }
  20. 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.
  21. FOR is great for iterating through arrays WHILE is great

    for running code until a condition is met
  22. $fruitArray = [ ‘apple’, ‘banana’, ‘orange’ ]; for($i = 0;

    $i < count($fruitArray); $i++) { echo “Current fruit: “ . $fruitArray[$i]; }
  23. $fruitArray = [ ‘apple’, ‘banana’, ‘orange’ ]; $notOrange = true;

    $count = 0; while($notOrange) { if($fruitArray[$count] === ‘orange’) { $notOrange = false; } echo $fruitArray[$count] . “is not an orange.”; $count++; }
  24. 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 ( ).
  25. function fruitFunction() { $fruitArray = [ ‘apple’, ‘banana’, ‘orange’ ];

    for($i = 0; $i < count($fruitArray); $i++) { echo ‘Current fruit: ‘ . $fruitArray[$i]; } } fruitFunction(); //this triggers our code above
  26. 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
  27. 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/