Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions arrays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

$arr = ["text", "random"];
$arrWhoNum = [1, 2, 6, 0, 5];
$arrMulti = ["multidimension", $arr, $arrWhoNum];

echo count($arr); // 2;
echo "<hr>";

$arrMerge = array_merge($arr, $arrWhoNum);
echo"<pre>";
print_r($arrMerge);
echo"</pre>";
echo "<hr>";

echo end($arrWhoNum); // 5
echo "<hr>";

$arrAdd = $arrWhoNum;
array_push($arrAdd, 9);
echo"<pre>";
print_r($arrWhoNum); // origin
print_r($arrAdd); // 9 pushed
echo"</pre>";

?>
60 changes: 60 additions & 0 deletions conditionals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

$date = new DateTime;
if ($date->format( 'N' ) == 1) {
echo "We are on Monday";
} else {
echo "We are not on Monday";
}

echo "<hr>";

if ($date->format( 'm' ) == 10) {
echo "We are in October";
} else {
echo "We are not in October";
}

echo "<hr>";

if ($date->format( 'i' ) < 10) {
echo "The current minute is less than 10";
} else if ($date->format( 'i' ) > 15) {
echo "The current minute is more than 15";
} else {
echo "Does not meet any conditions";
}

echo "<hr>";

switch ($date->format( 'N' )) {
case '1':
echo "Today is a working day";
break;

case '2':
echo "Today is a working day";
break;

case '3':
echo "Today is a working day";
break;

case '4':
echo "Today is a working day";
break;

case '5':
echo "Today is a working day";
break;

case '6':
echo "Today is not a working day";
break;

case '7':
echo "Today is not a working day";
break;
}

?>
10 changes: 10 additions & 0 deletions dates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$date = new DateTime;
echo $date->format("Y-m-d") . "<br><hr>"; // <-- Current date YYYY-MM-DD format
echo $date->format("m-Y-d \T H:i:s") . "<br><hr>"; //<-- Different format w/ time
echo $date->format("d") . "<br><hr>"; // <-- Today's day
echo $date->format("m") . "<br><hr>"; // <-- Today's month
echo $date->format("i") . "<br><hr>"; // <-- Current minutes

?>
55 changes: 55 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

function add($x, $y) {
return $x + $y;
}
echo add(2, 3); // 5
echo "<hr>";

function multiply($x, $y) {
return $x * $y;
}
echo multiply(2, 3); // 6
echo "<hr>";

function divide($x, $y) {
return $x / $y;
}
echo divide(2, 3); // 0.66667
echo "<hr>";

function operate($op, $x, $y) {
switch ($op) {
case 'add':
return add($x, $y);
break;

case 'subtract':
return $x - $y;
break;

case 'multiply':
return multiply($x, $y);
break;

case 'divide':
return divide($x, $y);
break;

case 'power':
return $x ** $y;
break;

case 'root':
return $x ** (1/$y);
break;

default:
return "Error: Invalid arguments";
break;
}
}
echo operate("root", 32, 5); // 2
echo "<hr>";

?>
38 changes: 38 additions & 0 deletions iterators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

for ($i = 0; $i <9; $i++) {
$solarSystem = ["Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"];
if ($i == 0) {
echo "<h2>$solarSystem[$i]</h2><ol>";
} else if ($i == 8) {
echo " <li>$solarSystem[$i]</li></ol>";

} else {
echo " <li>$solarSystem[$i]</li>";
}
}

echo "<hr>";

$a = [1, 2, 3, 4, 5, 6, 7, 8, 4, 9];
$aTimes2 = [];
foreach ($a as $n) {
array_push($aTimes2, $n*2);
}
echo "<pre>";
print_r ($a);
print_r ($aTimes2);
echo "</pre>";

echo "<hr>";

$i = 0;
while ($i < 1) {
echo "- <q>What's tatoes, precious? Tell us!</q> <br><br>";
$i++;
}
do {
echo "- <q>Po-ta-toes! Boil'em, mash'em, stick'em in a stew - even you couldn't say no to that!</q>";
} while ($i < 0);

?>
44 changes: 44 additions & 0 deletions maths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

$a = -3.87;

$abs = abs($a);
echo abs($abs); // 3.87
echo "<hr>";

$rou = round($a);
echo round($rou); // -4
echo "<hr>";

function highestNum () {
$nums = func_get_args();
$highestNum = $nums[0];
foreach ($nums as $n) {
if ($n > $highestNum) {
$highestNum = $n;
}
}
return $highestNum;
}
$result = highestNum(2, 4, 6, 2, 7, 4, 8, 5);
echo $result; // 8
echo "<hr>";

function lowestNum () {
$nums = func_get_args();
$lowestNum = $nums[0];
foreach ($nums as $n) {
if ($n < $lowestNum) {
$lowestNum = $n;
}
}
return $lowestNum;
}
$result = lowestNum(2, 4, 6, 2, 7, -4, 8, 5);
echo $result; // -4
echo "<hr>";

$ran = rand();
echo $ran; // <-- random integer

?>
33 changes: 33 additions & 0 deletions operators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

$a = 3;
echo $a + (1 - 3) * 3 / 2 * $a; // -6
echo "<br>";
echo $a % 5; // 3

echo "<hr>";

$b = 2;
var_dump($a == $b); // bool(false)
var_dump($a != $b); // bool(true)
echo $a > $b; // 1
echo $a < $b; // *Nothing is displayed when a false boolean is echoed.
var_dump($a <= ($b + 1)); // bool(true)
echo $a >= $b; // 1

echo "<hr>";

$c = false;
$d = true;

if ($c && $d ) {
echo "Both true";
} else if (!$d) {
echo "D is not true";
} else if ($c xor $d) {
echo "One is true, not both."; // <-- First valid
} else if ($c || $d) {
echo "Any is true"; // <-- Also valid, but code was broken.
}

?>
5 changes: 5 additions & 0 deletions phpinfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

phpinfo();

?>
24 changes: 24 additions & 0 deletions print.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

$a = "Testing PHP";
print $a;
echo "$a <br><br>" . $a; // Starts adjacent to previous "print"

echo "<hr>";

function test() {
$b = 2;
return $a + $b;
}
echo test(); // Drops error because $a is not in the scope of the function
// BUT gives "2" stored in $b

echo "<hr>";

$b = ["This is a test", 2, true];

echo "<pre>";
print_r($b);
echo "</pre>";

?>
41 changes: 41 additions & 0 deletions strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

echo "text";
echo "<hr>";

$a = "more";
echo "$a text"; // more text
echo "<hr>";

echo $a . " and $a text"; // more and more text
echo "<hr>";

$b = "This is a silly example";
echo str_replace("silly", "test", $b); // <-- This is a test example (Replace is case sensitive)
echo "<hr>";

$b = "This is a SILLY example";
echo str_ireplace("silly", "test", $b); // <-- This is a test example (Ireplace is case INsensitive)
echo "<hr>";

$c = "this is some text, ";
echo str_repeat($c, 3); // <-- this is some text, this is some text, this is some text,
echo "<hr>";

echo "'$a' is " . strlen($a) . " characters long"; //<-- 'more' is 4 characters long
echo "<hr>";

echo strpos($a, "o"); // <-- 1
echo "<hr>";

$A = strtoupper($a);
echo $A; // <-- MORE
echo "<hr>";

echo strtolower($A); // <-- more
echo "<hr>";

echo substr($a, -2); // <-- re
echo "<hr>";

?>
13 changes: 13 additions & 0 deletions types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$boolean = true;
$integer = 3;
$float = 3.0;
$string = 'Hey';
$array = [1, "2", true];
class potato {};
$object = new potato;
$null = null;
echo gettype($null); // <-- Insert variable to check its type

?>