Skip to content

Instantly share code, notes, and snippets.

@mrmnmly
Last active May 23, 2017 08:59
Show Gist options
  • Save mrmnmly/65a7ee207b7850ad5d0b7d8ff6818017 to your computer and use it in GitHub Desktop.
Save mrmnmly/65a7ee207b7850ad5d0b7d8ff6818017 to your computer and use it in GitHub Desktop.
Learning PHP
<?php
$fruits = array("bananas", "apples", "pears");
echo 'I love eating ' . $fruits[1] . ' too!';
?>
<?php
// On the line below, create your own associative array:
$myArray = array(1, 2, 3, 4);
// On the line below, output one of the values to the page:
echo $myArray[0];
// On the line below, loop through the array and output
// *all* of the values to the page:
foreach($myArray as $item){
echo $item . "</br>";
}
?>
<?php
// This is an array using integers as the indices...
$myArray = array(2012, 'blue', 5);
// ...and this is an associative array:
$myAssocArray = array('year' => 2012,
'colour' => 'blue',
'doors' => 5);
// This code will output "blue"...
echo $myArray[1];
echo '<br />';
// ... and this will also output "blue"!
echo $myAssocArray['colour'];
?>
<?php
$food = array('pizza', 'salad', 'burger');
$salad = array('lettuce' => 'with',
'tomato' => 'without',
'onions' => 'with');
// Looping through an array using "for".
// First, let's get the length of the array!
$length = count($food);
// Remember, arrays in PHP are zero-based:
for ($i = 0; $i < $length; $i++) {
echo $food[$i] . '<br />';
}
echo '<br /><br />I want my salad:<br />';
// Loop through an associative array using "foreach":
foreach ($salad as $ingredient=>$include) {
echo $include . ' ' . $ingredient . '<br />';
}
echo '<br /><br />';
// Create your own array here and loop
// through it using foreach!
$arr = array(1 => "a", 2 => "b", 3 => "c", 4 => "d");
$lng = count($arr);
foreach($arr as $el=>$i) {
echo $i . ' elem is ' . $el;
}
?>
<?php
$deck = array(array('2 of Diamonds', 2),
array('5 of Diamonds', 5),
array('7 of Diamonds', 7),
array('bla', 9));
// Imagine the first chosen card was the 7 of Diamonds.
// This is how we would show the user what they have:
echo 'You have the ' . $deck[2][0] . '!';
?>
<?php
class Person{
// static functions can be executed without creating new instance of class
public static function say(){
echo "Here are my thoughts!";
}
}
class Blogger extends Person{
// constants can be accessed without creating new instance of class
const cats = 50;
}
// executing static function directly from class definition (without created any class intance)
Blogger::say();
// getting constant variable directly from class definition (without creating any class instance)
echo Blogger::cats;
?>
<?php
class Dog {
public $isAlive = true;
function __construct($name) {
this->$name = $name;
}
public function bark() {
echo 'Bark! Bark!';
}
// final keyword prevents from overriding it by child class
public final function eat() {
echo 'Yummy!';
}
}
class Puppy extends Dog {
// will override dog function
public function bark() {
echo 'Woof! Woof!';
}
// will not override parent class because of final keyword
public function eat() {
echo 'Mfmfmfmfmf!';
}
}
$animal = new Puppy('Rex');
$animal->bark(); // will return 'Woof! Woof!'
$animal->eat(); // will return 'Yummy!'
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment