Basic PHP Structure:
<?php
// This is a comment
echo “This is my first php file!”;
?>
Variables:
<?php
// Variables use Dollar Signs $
$first_name = “John”;
// Numbers don’t use quotation marks
$favorite_number = 41;
?>

Math Operators:
+
*
/
%
**

Assignment Operators
=
+=
-=
*=
/=
%=


Comparison Operators
==
===
!=
<>
!==
>
<
>=
<=

Increment Operators
++$variable
$variable++
— $variable
$variable —

Logical Operators
And
Or
Xor
&&
||
!

Basic IF Statement
<?php
if (condition) {
// do something
}
Example:
if (2 > 1) {
Echo “2 is greater than 1”;
}
?>

If/Else/Elseif
<?php
if (condition) {
// do something
} elseif (condition) {
// do something
} else {
// do something
}
?>
Example:
<?php
if ($variable > 100) {
echo “Your variable is greater than 100”;
} elseif ($variable == 150) {
Echo “Your variable is 150!”;
} else {
“Your number is less than 100”;
}
?>

Arrays

<?php
// Numeric Array
$my_array = array(“Blue”, “Red”, “Green”, “Purple”);
Echo $my_var[0];
// echoes out Blue
?>
<?php
// Associative Arrays
$ages = array(“John”=>”39", “Bob”=>”18", “Mary”=>”29");
echo $ages[“John”];
// echoes out 39
?>

While Loop
<?php
$counter = 0;
while ($counter < 10) {
echo $counter;
$counter++;
}
?>
For Loop
<?php
for ($counter = 1; $counter < 10; $counter++){
echo $counter;
}
?>
Functions
<?php
Function namer($names) {
echo “Hello, my name is “ . $names;
}
namer(“John Elder”); // call the function
?>
Random Numbers
<?php
echo rand(1, 100);
// creates random number between 1 and 100
?>
Date Function
<?php
echo date (‘l js \of F, Y);
// echoes out date in format Tuesday 26th of January 2018
?>

String Manipulation
<?php
$variable = “John Elder is a Dork”;
str_replace(“Dork”, “God”, $variable); // replace Dork with God
echo strtoupper($variable);
echo ucwords($variable);
echo strtolower($variable);
echo lcfirst($variable);
echo
// Capitalize all letters
// Capitalize first letter of all words
// Lowercase all letters
// Lowercase only first word
?>
Include Function
<?php
Include(‘whatever.php’);
?>
Require Once Function
<?php
require_once(‘navbar.php’);
?>

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store