Table of Contents
PHP Coding Conventions
- All PHP files must end in .php
- Statements (or commands) in PHP end with a semicolon (;)
- If you want to include comments, which are nonfunctional notes, you can do so in two way
// Single-line comments are two forward slashes.
/* Multiline, or block comments, start with a forward slash and an asterisk. They end
with an asterisk and forward slash. */
Variables
A way to store information in order to reference it later
Defining a Variable
It should start with a dollar sign $ following an alphanumeric name.
- For variable names with more than one word, it’s a good practice to us underscores between the words
Variable Types
We do not need to declare what kind of data our variable are most of the time. This makes PHP a weak typed language.
- Integers: Whole numbers (for example, 0, 45, or 128)
- Floats: Numbers with decimal points (for example, 1.0 or 3.16)
- Characters: Single letters, numbers, or symbols
- Strings: Collections of characters (for example, ‘Hello’ or ‘Racer 5’
- Booleans: True or False
Some points:
- 1 vs. “1”: Numbers with and without quotes are
handled differently. You can do calculations with 1, but not ‘1’. - ‘A’ vs. ‘a’: Uppercase and
lowercase letters are represented differently in PHP. - Arrays: Arrays are another variable type you’ll
learn all about later.
Strings in PHP
Double Quotes: Strings in double quotes will be processed by PHP before they’re outputted.
Arrays
'Hoomaan',
'age' => 43,
'job' => 'Front-end developer',
);
echo $hoomaan['job']; // Front-end developer
Arithmetic Operators and Math in PHP
- Addition with +
- Subtraction with –
- Multiplication with *
- Division with /
- Get the remainder with %
PEMDAS
- Parentheses
- Exponents
- Multiplication and division
- Addition and subtraction
Comparison Operators
Allow us to compare values and evaluate them as true or false.
// Less than comparison: <
// Not or Negation: !
// Not equal: !=
// Not Identical: !==
Logical Operators
Control Structures
- A single if always goes first
- The first condition to evaluate as true will be the one that runs. The rest will be ignored
- The else statement always goes last—it serves as a catchall if none of the conditions are true
Welcome to the home page of my website!
Have a look around.
Yoda Conditionals
It’s recommended to use Yoda conditionals when you’re going to check the equality of a variable.
Put the variable at the right side of the conditions. This will prevent accidental assignment and bugs in your code.
Loops
While loops vs Foreach loops
While loops: “Do something while the condition is true”
- General-purpose loops used for math, enumeration, or anything you can think of
Foreach loops: “Do something for each item in a list”
- Used to process arrays
// While Loop
while ( condition is true ) {
/*Code to execute. Should include to eventually make condition false */
}
// Foreach Loop
$items = array ('1', '2', '3');
foreach ( $items as $item ) {
// Do something with next item in the array
}
$colors = array( 'best' => 'red', 'better' => 'blue', 'good' => 'green', 'ok' => 'yellow' );
foreach ( $colors as $rank => $color ) {
echo "$color is $rank \n";
}
Functions
Reusable snippets of code that can be called multiple times
function hello_world () {
return "Hello World";
}
echo ''. hello_world() . '
';
// Passing Arguments
function is_bigger($a, $b) {
return $a >= $b;
}
$bigger = is_bigger (10, 5);
WordPress Functions file
Class
A code definition or template for creating objects
Object
A group of variables and functions marked by a single identifier.
Hooks
Allow us to insert our own code into WordPress without modifying “core”
There are two types of hooks:
- Actions: Changes how WordPress does something (imagine adding air conditioning to your house)
- Filters: Modifies the information WordPress retrieves (image painting your house