Intro to PHP and MySQL
Class 1
Welcome!
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
- We are here for you!
- Every question is important
- Help each other
- Have fun
Welcome!
Tell us about yourself.
- Who are you?
- What programming experience do you have?
- What do you hope to get out of the class?
- If you were a super hero, what would your super power be?
Tell us about yourself.
- Who are you?
- What programming experience do you have?
- What do you hope to get out of the class?
- If you were a super hero, what would your super power be?
Principles of Development
DRY - Don't Repeat Yourself
Be Lazy
Trust no user (everyone is trying to exploit you)
Downloads
- VirtualBox
- Vagrant
- SublimeText 3 or Atom
- Chrome
- JSONView
- var_dumpling()
- Web Developer
-
GIT Mac / GIT Windows
(recommended)
Why not use M(W)AMP?
Everyone on the same setup
Mirror the dev/production environment
Multiple environments on the same machine
Resources
Setup
cd ~
git clone git@github.com:nickdenardis/gdi-core-php.git
cd gdi-core-php/lessons
vagrant up
Visit http://10.10.10.10/PHP
PHP is a popular general-purpose scripting language that is especially suited to web development.
“PHP: Hypertext Preprocessor”
Server-side language
What can it do?
- Dynamic generation of web-page content
-
Database interaction
-
Processing of user supplied data
-
Email
-
File handling
-
Text/Image processing
-
Network interaction
- And more…
Use case
Let's begin
- ~/gdi-core-php/lessons/class1/
- Rename index.html -> index.php
- Add a PHP Block to the top
-
<?php ?> <!DOCTYPE html> ...
Manual: http://www.php.net/manual/en/language.basic-syntax.phptags.php
Note about PHP short tags
Preferred
<?php
...
?>
Not Preferred
<?
...
?>
Displaying text
<?php
echo 'Text to display';
print 'More text';
?>
<!DOCTYPE html>
...
We will use 'echo' for simplicity
Manual: http://php.net/echo
Single/double quotes
<?php $great = 'fantastic'; echo 'This is {$great}'; // Single quotes are literal
// This is {$great}
echo "This is {$great}"; // Double quotes allow you to evaluate variables // This is fantastic
?>
Comments
<?php
// This is a single line comment
echo 'Text to display';
/* This is a comment
that is spread over
multiple lines. */
print 'More text';
?>
<!DOCTYPE html>
...
Escaping Characters
<?php
/* Use the backslash (\) to tell PHP
to 'ignore' (not interpret) the next character */
echo 'It's time to party!'; // Error
echo 'It\'s time to party!'; // Correct
?>
<!DOCTYPE html>
...
PHP + HTML
<?php
<address class="copyright">
<span class="adr">
<span class="street-address"><?php echo $address; ?></span><br />
<span class="locality"><?php echo $locality; ?></span>,
<span class="region"><?php echo $region; ?></span>
<span class="postal-code"><?php echo $post_code; ?></span>
</span>
<span class="tel"><?php echo $phone; ?></span>
</address>
Variables
<?php $a_bool = true; // a boolean $a_bool = false; // a boolean $an_int = 12; // an integer
$a_float = 12.345 // a float $a_str = "foo"; // a string $a_str2 = 'foo'; // a string $an_array = array(1, 2, 3, 4, 5); // an array $an_object = new Person; // an object $a_null = NULL // a null value
Dynamically typed
<?php
$a_string = 'Girl Develop It';
echo $a_string;
// Girl Develop It
$a_string = 15;
echo $a_string;
// 15
Type casting
<?php
echo (string)$a_string;
Using variables
Define and change at will
<?php
$var = 'Bob';
$Var = 'Joe';
echo "$var, $Var"; // outputs "Bob, Joe"
echo "{$var}, {$Var}"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number
$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228. ($filling = 'strawberries')
Viewing what variables contain
<?php
$some_variable = 'Contains some text';
var_dump($some_variable);
// print_r($some_variable);
Results:
Manual: http://php.net/var_dump
Concatenation
<?php
$class = 'Girl Develop It';
$topic = 'PHP & MySQL';
$attending = $class . ' learning ' . $topic;
echo $attending; // Girl Develop It learning PHP & MySQL
Constants
Define once, never change
<?php
// Valid constant names
define("FOO", "something");
define("FOO2", "something else");
define("FOO_BAR", "something more");
// Error, already defined
define("FOO", "change something");
// Invalid constant names
define("2FOO", "something");
// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__", "something");
Using constants
Use like any other variable
<?php
define('MY_HOMETOWN', 'Detroit');
echo 'Anyone else from ' . MY_HOMETOWN . '?';
Results:
Cannot redefine
<?php
define('MY_HOMETOWN', 'Detroit');
define('MY_HOMETOWN', 'Chicago');
echo 'Anyone else from ' . MY_HOMETOWN . '?';
Results:
Is defined?
<?php defined('MY_HOMETOWN'); // returns boolean // Safe way to define if ( ! defined('MY_HOMETOWN') ) { define('MY_HOMETOWN', 'Detroit'); }
Manual: http://php.net/defined
Arithmetic
Example | Name | Result |
---|---|---|
-$a | Negation | Opposite of $a . |
$a + $b | Addition | Sum of $a and $b . |
$a - $b | Subtraction | Difference of $a and $b . |
$a * $b | Multiplication | Product of $a and $b . |
$a / $b | Division | Quotient of $a and $b . |
$a % $b | Modulus | Remainder of $a divided by $b . |
$a ** $b | Exponentiation | Result of raising $a to the $b 'th power. Introduced in PHP 5.6. |
Examples
<?php
echo (5 + 3) . '<br />'; // prints 8
echo (5 - 3) . '<br />'; // prints 2
echo (5 * 3) . '<br />'; // prints 15
echo (5 / 3) . '<br />'; // prints 1.6666...
echo (5 % 3) . '<br />'; // prints 2
Result:
Conditionals
<?php
$color = 'red';
if ($color == 'red')
{
echo 'Red is my favorite color too!';
}
if ($color === 'green')
{
echo 'Such a boring color.';
}
else
{
echo 'Your color must be sweet!';
}
Conditionals
<?php $color = 'red'; $sky_color = 'blue'; if ($color == $sky_color)
{
echo 'Look up!';
}
All the comparison operators
Example | Name | Result |
---|---|---|
$a == $b | Equal | TRUE if $a is equal to $b after type juggling. |
$a === $b | Identical |
TRUE if $a is equal to $b, and they are of the same
type.
|
$a != $b | Not equal | TRUE if $a is not equal to $b after type juggling. |
$a <> $b | Not equal | TRUE if $a is not equal to $b after type juggling. |
$a !== $b | Not identical |
TRUE if $a is not equal to $b, or they are not of the same
type.
|
$a < $b | Less than | TRUE if $a is strictly less than $b. |
$a > $b | Greater than | TRUE if $a is strictly greater than $b. |
$a <= $b | Less than or equal to | TRUE if $a is less than or equal to $b. |
$a >= $b | Greater than or equal to | TRUE if $a is greater than or equal to $b. |
"Juggling"
PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used.
<?php
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
Switch
<?php
$color = 'blue';
switch ($color) {
case: 'red':
echo 'Great color!';
break;
case: 'blue':
echo 'That\'s an OK color.';
break;
case 'green':
case 'pink':
case 'yellow':
echo 'Really? You like one of those?';
break;
default:
echo 'Pick something more unique!';
break;
}; // notice the ;
Arrays
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
var_dump($array);
?>
Results:
Arrays of Arrays
<?php $array = array( "foo" => "bar", "bar" => array(
"baz" => "bat" ) ); var_dump($array); ?>
Loops
for
foreach
do-while
do
while
break
continue
For
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
Foreach
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
$value = $value * 2;
}
do-while
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
do
<?php
do {
if ($i < 5) {
echo "i is not big enough";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
echo "i is ok";
/* process i */
} while (0);
while
<?php
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
Including other files
<?php
require '/path/to/file.php'; // Will throw E_COMPILE_ERROR if not found
include '/path/to/file.php'; // Will throw E_WARNING if not found but will continue
require_once 'path/to/file.php';
include_once 'path/to/file.php';
Let's try it!
Break the single page into modules
Problem 1:
You have to add another page to the 'Women in Computing' website.
Task:
Create an easy way to add pages
PHP & MySQL - Class 1 - Girl Develop It
By Nick DeNardis
PHP & MySQL - Class 1 - Girl Develop It
- 1,797