Created
July 28, 2018 21:20
-
-
Save EvanDotPro/6664f43ab83bb904732308ea55980c4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// All PHP files start with <?php | |
// Two slashes is a comment, as you can see. | |
// We are going to start with variables. | |
// Variables are just like, a "container" to hold values. | |
// All variables in PHP start with a $ sign. | |
// Their names must start with a letter, not a number | |
$name = "Max"; // This is a string | |
$age = 14; // This is an integer | |
// Let's print something to the screen! | |
echo "Hello, my name is $name and I am $age years old.\n"; | |
// You can also do math with multiple integers. | |
echo "In 2 years, I will be " . ($age + 2) . " years old.\n"; | |
// So, why did we need string concatenation here? I'll show you. | |
echo "In 2 years, I will be $age+2 years old.\n"; | |
// You might expect the above to work, but it will print literally 14+2 | |
// Here's another way we could have done it: | |
$futureAge = $age + 2; | |
echo "In 2 years, I will be $futureAge years old.\n"; | |
// So that's basic variables. | |
// Another type of variable in PHP is called an Array. | |
// Arrays are just lists of values. | |
$ochatdMembers = ['Max', 'Shep', 'Shdwtek', 'beachpeach']; | |
echo "Again, my name is..." . $ochatdMembers[0] . "\n"; | |
echo "My favorite person in the chat is... " . $ochatdMembers[1] . "\n"; | |
echo "Here's a list of my friends from the chat:\n"; | |
// We can add to an array after it's been created: | |
$ochatdMembers[] = "tweedsheep"; | |
// Notice i didn't put a number between the [] | |
// That's because PHP will automatically assign it to the next | |
// highest key value. In this case it would be 4 because | |
// we already have 0, 1, 2, and 3 assigned above. | |
$ochatdMembers[5] = "nezelbert"; // You can also specify a specific key too. | |
// Now let's make the list numbered! | |
foreach ($ochatdMembers as $i => $member) { | |
// Now in the loop, $i will be our array key | |
// But we don't want the list to start at 0... | |
echo " " . ($i + 1) . ". $member\n"; | |
} | |
echo "...Yes, I'm friends with myself.\n"; | |
echo "I have " . count($ochatdMembers) . " friends in the chat.\n"; | |
// Array keys don't have to be numbers. They can also be strings. | |
$ochatdAges = [ | |
"Shep" => 28, | |
"Max" => 14, | |
"Shdw" => 36, | |
"Nez" => 28, | |
]; | |
// We can add a new key like this after the array has been made: | |
$ochatdAges["Faith"] = 21; | |
function addTwo($age) { | |
return $age + 2; | |
} | |
function printName($name, $age, $term = "is") { | |
echo " - $name $term $age years old"; | |
if ($age > 25) { | |
echo " (old fart)"; | |
} | |
echo "\n"; | |
} | |
echo "Here's a list of their ages:\n"; | |
foreach ($ochatdAges as $name => $age) { | |
printName($name, $age); | |
} | |
echo "In two years, this is how old everyone will be:\n"; | |
foreach ($ochatdAges as $name => $age) { | |
printName($name, addTwo($age), "will be"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment