The document discusses PHP concepts like syntax, data types, variables, arrays, operators, loops, functions, object-oriented programming, classes and methods. It provides code examples for filtering bad words in a string, calculating the sum of digits in an array, and creating a Society class with methods to set and get its name. Real-world uses of PHP concepts are demonstrated, including extending the Society class to output its name when cast to a string.
2. Structure of Program
● Syntax
– Code between <?php tags ?>
– Statements end with “;”
– Comments
●
// Single line
●
/* Multi line */
● Sample
– <?php echo “Hello, world”; ?>
@EdiPHP
http://bit.ly/EdiPHP
3. Data Types
● Boolean – True/False
● Int – signed numeric integer
● Float – floating-point
● String – a collection of binary data
– Note Quotes: “this” is different from 'this'
● A bit.
@EdiPHP
http://bit.ly/EdiPHP
4. Variables
● Starts with $ sign
– Followed with a letter or “_”
● Dynamically typed
– <?php $foobar = “barfoo”; $foobar = 1; $foobar =
true; ?>
● Defining if a variable exists
– isset($var)
@EdiPHP
http://bit.ly/EdiPHP
5. Arrays
● Can be constructed following ways:
●
$var = Array(1,2,3,4,5,6);
●
$var = Array(‘cat’ => ‘lolcat’, 0 => 4);
● Or:
$var = Array();
$var[] = 'A';
$var[] = 'B';
●
Is equivalent to $var = Array (‘A’, ‘B’, ‘c’);
$var[] = 'c';
@EdiPHP
http://bit.ly/EdiPHP
6. Operators
● Standard ones (+,-,/,*)
● Comparison (==,>=, <=,!=, ===, !==)
● Concatenation .
– “foo”.”bar” == “foobar”
● What happens if you do:
– “2” + 5
@EdiPHP
http://bit.ly/EdiPHP
7. Loops
● For
– for ($i=0; $i < 4; $i++) { echo $i; }
● While
– while (true) { destroyTheUniverse(); }
● Do .. while
– do { destroyTheUniverse(); } while (false);
● Foreach
– foreach ($arr = Array(‘a’ => ‘b’, ‘c’ => ‘d’) as $key =>
$value) { echo $key.$value; }
@EdiPHP
http://bit.ly/EdiPHP
8. Functions
● Begin with reserved word “f u n ct ion ”
● Can have 0 or more formal parameters
● Can have optional parameters:
<?php
function foobar ($reqParam, $optParam = “bar”)
{
echo $reqParam.’ foo’.$optParam;
}
?>
@EdiPHP
http://bit.ly/EdiPHP
9. OOP
● PHP has objects like java, cpp does
●
Reserved word for class is “class”
● Have good old public, private, etc. and static,
final, etc. methods
@EdiPHP
http://bit.ly/EdiPHP
10. Structure of a class
<?
class foobar
{
private $privVar = 15;
public function getPrivVar ()
{
return $this->privVar;
}
}
@EdiPHP
?> http://bit.ly/EdiPHP
11. Real world examples (1)
● Bad word filtering
– A function that converts all occurrences of
“LOLSoc” to “CompSoc”
<?php
function filterBadWords($input)
{
return str_replace(‘LOLSoc’, ‘CompSoc’, $input);
}
?>
@EdiPHP
http://bit.ly/EdiPHP
12. Real World examples (2)
● Calculating sum of digits in array
<?php
$arr = array(1,2,3,4,5,6,7,-6);
function sumDigits ($a)
{
$sum = 0;
for ($i=0; $i < count($a); $i++)
{
$sum += $a[$i];
}
return $sum;
@EdiPHP
}
http://bit.ly/EdiPHP
13. Real World Examples (3)
● Creating a class society
– Which has a name
● That is a useless class, though, that's the best
that fits into slide :)
@EdiPHP
http://bit.ly/EdiPHP
14. <?php
Real World Examples (3)
Class society
function setName($name)
{ {
private $_name; $this->$_name = $name;
function setName($name)
return $this;
}
{
$this->$_name = $name;
public function getName()
return $this;
}
public function getName() {
{
return $this->getName();
}
return $this->getName();
}
public function __construct($name)
{
$this->setName($name);
}
}
@EdiPHP
?> http://bit.ly/EdiPHP
15. Extending the previous
example
● Making it echo
● Add following method to class:
public function __toString()
{
echo $this->getName();
}
@EdiPHP
http://bit.ly/EdiPHP
16. A bit about static methods
● To define a static function:
public static function foobar ()
{
return “foobar”;
}
● To call a static function
– From outside the class scope: className::function()
– From inside the class: self::function()
@EdiPHP
http://bit.ly/EdiPHP