PHP Cheatsheet Cheatsheet




Basics

Sample Program

<?php
	echo "Hello, world!!!!"
?>
  • echo : echo is used to output data to the console/screen.
  • print : print is also used to output data to the screen. Both echo and print are more or less same with small differences, echo doesn't have a return value but print has a return value. echo can have multiple arguments but print takes only one argument. Echo is faster than print comparatively.
  • fscanf : fscanf is used to read input from console
  • // : single line comment
  • # : Single line comment
  • /* */ : Multi line comment

Data Types

PHP is a loosely typed language and hence you no need to declare variables with data types.

However, below are the different datatypes in PHP.

Data typeUsage
Integer$x = 999999;
String$str = "Hello World"
Float$x = 79.53
Boolean$isAvailable = TRUE;
Array$arr = array("iPhone", 1000, TRUE);
Object$obj = new className();
Null$str = null;
Resource$conn = mysqli_connect(localhost,"root","userName","password");

Variables

$variable-name = value;  

Constants

Using define()

define(name, value, case-insensitive)
  • name: name of the constant
  • value: value of the constant
  • case-insensitive: Specifies whether the constant name should be case-insensitive or not. By default it is set to false.

Using const keyword

const MESSAGE = "Happy learning!";

String Functions

String FunctionUsage
strlen()echo strlen("One Compiler");
str_word_count()echo str_word_count("Hello world! Happy Learning!!");
strrev()echo strrev("One Compiler");
str_replace()echo str_replace("bar", "Foo", "Good morning bar!");
strpos()echo strpos("Hello world! Happy Learning!!", "appy");

Operators

Operator typeDescription
Arithmetic Operator+ , - , * , / , %, ++, --
comparision Operator< , > , <= , >=, != , ==
Logical Operator&& , ||, !
Assignment Operator= , += , -= , *= , /= , %=
Conditional Operator? :

Arrays

$arrName = array(values..);

1. Indexed arrays

$directions = array("East", "West", "North", "South");

2. Associative arrays

$capitals = array("Japan" => "Tokyo", "India" => "New Delhi","United Kingdom" => "London","United States" => "Washington, D.C.","China" => "Beijing");

3. Multi dimensional arrays

// two-dimensional array
$num = array(
  array(1,2,3),
  array(4,5,6),
  array(7,8,9)
  );

Conditional Statements

1. If

if(conditional-expression)
{
    //code
}

2. If-else

if(conditional-expression)
{
    //code
} else {
    //code
}

3. If-else-if

if(conditional-expression-1)
{
    //code
} else if(conditional-expression-2) {
    //code
} else if(conditional-expression-3) {
    //code
}
....
else {
    //code
}

4. Nested-If

if(conditional-expression-1) {    
     //code    
          if(conditional-expression-2) {  
             //code
             if(conditional-expression-3) {
                 //code
             }  
    }    
}

5. Switch

switch(conditional-expression){    
case value1:    
 //code    
 break;  //optional  
case value2:    
 //code    
 break;  //optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

Loops

1. For

for(Initialization; Condition; Increment/decrement){  
//code  
} 

2. Foreach

foreach ($array as $value) {
  //code 
}

3. While

while(condition){  
//code 
}  

4. Do-while

do{  
//code 
}while(condition); 

Functions

function functionName() { // defining a function
  //code
}

functionName (parameters); //calling a function 


┬ęCopyright 2020 OneCompiler | Privacy Policy | Terms & Conditions