Frogg logo

Frogg's web tools

Hand Crafted Tools

Home > Coding PHP > PHP - Basics
Welcome on Frogg's web tools | Current date :
21/11/2024

PHP - Basics

Types

Official doc : types

Scalar types

  • boolean
  • integer
  • float (floating-point number, aka double)
  • String

Compound types

  • array
  • object
  • callable
  • iterable

Special types

  • resource
  • NULL

Pseudo-types

  • mixed
  • number
  • callback (aka callable)
  • array|object
  • void

Constants

Official doc : Constants
A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. copy to clipboard
define("CONSTANT", "Anything");
const CONSTANT="Anything";

Variables

Official doc : Variables
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

Functions

Official doc : Functions
A function may be defined using syntax such as the following: copy to clipboard
function foo($argument1, $argument2, /* ..., */ $argumentn){
	echo "Example function.\n";
	return $retval;
}

#variable function
$func = 'foo';
$func('$argument1');

Operators

Official doc : Operators
Official doc : Operator Precedence

Logical Operators

Official doc : Logical Operators
Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.

Bitwise Operators

Official doc : Bitwise Operators
Example Name Result
$a&$b And Bits that are set in both $a and $b are set.
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
$a ^ $b Xor (exclusive or) Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a << $b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two")
$a >> $b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")

Comparison Operators

Official doc : Comparison Operators
Example Name Result
$a==$b Equal TRUE if $a is equal to $b after type juggling.
$a===$b Identical TRUE if $a is equal to $b, and they are of the same type.
$a!=$b Not equal TRUE if $a is not equal to $b after type juggling.
$a<>$b Not equal TRUE if $a is not equal to $b after type juggling.
$a!==$b Not identical TRUE if $a is not equal to $b, or they are not of the same type.
$a<$b Less than TRUE if $a is strictly less than $b.
$a>$b Greater than TRUE if $a is strictly greater than $b.
$a<=$b Less than or equal to TRUE if $a is less than or equal to $b.
$a>=$b Greater than or equal to TRUE if $a is greater than or equal to $b.
$a<=>$b Spaceship An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.

Incrementing/Decrementing Operators

Official doc : Incrementing/Decrementing Operators
Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

Array Operators

Official doc : Array Operators
Example Name Result
$a + $b Union Union of $a and $b.
$a == $b Equality TRUE if $a and $b have the same key/value pairs.
$a === $b Identity TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
$a != $b Inequality TRUE if $a is not equal to $b.
$a <> $b Inequality TRUE if $a is not equal to $b.
$a !== $b Non-identity TRUE if $a is not identical to $b.

String Operators

Official doc : String Operators There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

Assignment Operators

Official doc : Assignment Operators The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

Execution Operators

Official doc : Execution Operators
PHP supports one execution operator: backticks ``.

Error Control Operators

Official doc : Error Control Operators
PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored

Type Operators

Official doc : Type Operators
instanceof is used to determine whether a PHP variable is an instantiated object of a certain class.

Control Structures

Official doc : Control Structures

if

copy to clipboard
if( condition ) { statement; }

if/else

copy to clipboard
if( condition ) { ifStatementIf; } else { elseStatementIf; }

while

copy to clipboard
while ( condition ) { statement; }

do-while

copy to clipboard
do { statement; } while ( condition );
  • the difference with "while" is that the "statement" is always executed the first time

for

copy to clipboard
for ( expr1 ; expr2 ; expr3 ) { statement; }
  • The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.
  • In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.
  • At the end of each iteration, expr3 is evaluated (executed).

foreach

copy to clipboard
foreach (array_expression as $value){ statement; }
copy to clipboard
foreach (array_expression as $key => $value){ statement; }

break

break ends execution of the current for, foreach, while, do-while or switch structure.

continue

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

switch

switch (testedValue) {
	case condition1: statement1;break;
	case condition2: statement2;break;
	case condition3: statement3;break;
	default: defaultStatement;
}

declare

copy to clipboard
declare ( directive ) { statement; }

return

return returns program control to the calling module. Execution resumes at the expression following the called module's invocation.

include

copy to clipboard
include filename;

include_once

The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE. As the name suggests, the file will be included just once.

require

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

require_once

The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.

goto

copy to clipboard
goto target;
 
target:
statement
Page created by the 11/02/2018 17:24
Generated in 0.001 sec & displayed in ... sec