PHP cheat sheet latest version

 PHP cheat sheet 8.1 (latest version)

Hello Every one Here i have created latest PHP cheat sheet as per my Knowledge. php latest version march 2022 is php 8.1.4


 

PHP Opening and closing tags

//opening tag

<?php

//closing tag

//?>

PHP functions used  to display the output


// echo is used to display text
echo "php cheat sheet";

//print_r() is used to display text and arrays
$array = array('1',2,'php cheatsheet');
print_r($array)

//var_dump is used for debugging
var_dump($array);

PHP to get input from user in console 



   //take input from user in console with out html
  $age = readline('What is php latest version?');
  

 Comments in PHP

Comments are used to understand the  purpose of writing the code snippets, it is very useful of writing 
comments to easily understand the code when you are working with team.
Comments are ignored by the compilers and interpreters.
 


echo 'php cheat sheet';
#double forward slashes for single line comment
$noncommented = 100;
//single line comments
//$commented = 100;

#comment using hash(#)
#another way of single line comment
#$anothervariable = 100;

#multiple line comment the code in between /* your code */ is not executed
/*multiple line comment*/
/*public function passion(){

 echo 'eat';
 echo 'code';
 echo 'repeat';
 passion();

}
passion();*/

Include files in php

The main advantage of using php files instead of html files is we can use a single file in multiple pages
No need to write the repeated code every time in all pages we can easily insert one file code into other page 
example : if we have a website containing  navbar, maincontent, and footer if the header and footer same for all pages  just make header and footer sections into two separate php files and include them in all pages.Here we are not writing the header and footer sections every time we are just including them
and another advantage is we can easily maintain the code
 

//we can insert a code of one file into other file using require,include,require_once,include_once;
//include throughs a warning if the included file doesn't exist it will insert a same file code as many times we need unlike include_once;
include 'header.php';
//require produces a fatal error(E_COMPILE_ERROR) 
//if the file doesnot exist and stops the execution of the file
require 'footer.php';

//include_once same as include but it checks for the same file already included in a file or not 
include_once 'footer.php';

//require_once same as require but it checks for the same file already included in a file or not 
require_once 'db.php';

DataTypes In PHP

Datatype describes the type of data, a type of data specifies the amount of data allocated to a current data

string

String is collection of characters or sequence of characters written in single or double quotes


//strings
// we can use single quote and double quotes for strings
$name = 'Jack' ;
$name = "Jackson";

// The Difference is double quote string can escape characters \n = new line  \t = tab  \\ = backslash
echo "Hello Mike\nHello David";

// In double quote string variables will be detected by PHP 
echo "Hello $name";

// string concatination in PHP
echo 'Hello ' . $name;

//explode function is used to convert a string into an array 
$variable = "One two three four and so on";
 
explode(" ",$variable);
//output is ['one','two','three','four','and','so','on']

//trim function is used to remove white space in before and after string
trim($variable);

// Convert a to lowercase or uppercase using following functions
strtolower($variable);
strtoupper($Fullname);
// Ucfirst function used to Convert the string of a first character to uppercase
ucfirst($name);   

//str_replace function is used to replace a string with desired string
$str = "Red Ball"; //replace Red  with Green
str_replace('Red','Green',$str); //output is Green Ball  
 

Integer

Intrger is a Number with out decimal positions


$number = 10; //integer
$number = 5.9; //float 
 
 

Array

Array is a variable which stores multiple value

 
echo $array; //not valid
//array index starts from 0
//indexed Array declaration and 
$language = ['python', 'PHP', 'ruby', 'java'];
// Accesing second element in array 
$language[1] //output is PHP 
//declaration of empty array
$language = array(); or $language[];
//inserting a element into an array
$language[] = 'Micheal'; 
// Multidimensional array
$array = array(
				array('a','b','c','d'),
				array('A','B','C','D'),
				array(1,2,3,4)
			);
$array[2][1] // B 
//remove specific element from array
unset($language['PHP']);  
//Array to string
echo implode(', ', $language) //output "Python, PHP, ruby, java"
// Length of an Array (or) no of elementsin an array
echo count($names); 
//array function to remove null values and we also can write callback function
array_filter($language); 
// Array merge
$array3 = array_merge($array1, $array2); 

 

Conditional Operators

Conditional operators are used when we need to output needs to meet some condtions
  

//if is  used to check for a condition
if(isset($var)){
echo $var;
} 
// If - elseif -  else is used to check for two or more conditions

if ($value == 3) {
    echo $value;
} elseif  ($value == 3) {
    echo $value;
} else {
    echo $value
}

// conditional Operators
//AND operator used when all conditions needs to  meet
if ($condition === 10 && $condition2 === 5) {
    echo '10 and 5'
}

// Or operator used when atleast one condition needs to meet
if ($condition === 10 || $condition2 === 5) {
    echo '10 or 5'
}
 
 //ternary operator
 
 echo ($value == 10) ? 'ten' : 'not ten' 

// Compare same variable with multiple values
switch ($value) {
    case '10':
        echo 'Value 10';
         break;
    case '20':
        echo 'Value is 20';
        break;
    case '30':
        echo 'Value is 30';
        break;
    default:
        echo 'Value is not  10,20,30';
} 
 

Loops

Loops are the iterate over a number or till a conditional met

//For loop is used to iterate over a known number
for ($i = 0; $i < 5; $i++) {
    echo i;
}

//while loop loops through a block of code as long as the specific condition true
$number = 1;
while ($number < 10) {
    echo 'value : ' . $number ;
    $number += 1;
}

//do while 
$number = 1;
do {
    echo 'value : ' . $number ;
    $number += 1;
} while ($number < 10);

//break is stops executions of a block  go out of the loop
//continue is escapes the current iteration and starts next iteration 

php latest version 8.1 4 features find here

Comments

  1. Great PHP cheat sheet thanks

    ReplyDelete
  2. one of the good php cheat i found in goodle

    ReplyDelete

Post a Comment

Popular posts from this blog

Have you upgraded your PHP version to 8.1.4?

Introduction to programming language