Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
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 is a popular general-purpose scripting language that is especially suited to web development.
“PHP: Hypertext Preprocessor”
Server-side language
<?php
?>
<!DOCTYPE html>
...
Manual: http://www.php.net/manual/en/language.basic-syntax.phptags.php
<?php
...
?>
<?
...
?>
<?php
echo 'Text to display';
print 'More text';
?>
<!DOCTYPE html>
...
<?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
?>
<?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>
...
<?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
<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>
<?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
<?php
$a_string = 'Girl Develop It';
echo $a_string;
// Girl Develop It
$a_string = 15;
echo $a_string;
// 15
<?php
echo (string)$a_string;
<?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')
<?php
$some_variable = 'Contains some text';
var_dump($some_variable);
// print_r($some_variable);
<?php
$class = 'Girl Develop It';
$topic = 'PHP & MySQL';
$attending = $class . ' learning ' . $topic;
echo $attending; // Girl Develop It learning PHP & MySQL
<?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");
<?php
define('MY_HOMETOWN', 'Detroit');
echo 'Anyone else from ' . MY_HOMETOWN . '?';
<?php
define('MY_HOMETOWN', 'Detroit');
define('MY_HOMETOWN', 'Chicago');
echo 'Anyone else from ' . MY_HOMETOWN . '?';
<?php defined('MY_HOMETOWN'); // returns boolean // Safe way to define if ( ! defined('MY_HOMETOWN') ) { define('MY_HOMETOWN', 'Detroit'); }
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. |
<?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
<?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!';
}
<?php $color = 'red'; $sky_color = 'blue'; if ($color == $sky_color)
{
echo 'Look up!';
}
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. |
<?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)
<?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 ;
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
var_dump($array);
?>
<?php $array = array( "foo" => "bar", "bar" => array(
"baz" => "bat" ) ); var_dump($array); ?>
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
$value = $value * 2;
}
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
<?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);
<?php
$i = 1;
while ($i <= 10) {
echo $i++; /* the printed value would be
$i before the increment
(post-increment) */
}
<?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';
Break the single page into modules