include(“file_name.php”);​
include_once(“file_name.php”);​
require(“file_name.php”);​
require_once(“file_name.php”);​
<?php
$_POST['variable_name'];
?>
<?php
$_GET['variable_name'];
?>
isset()
function checks if the particular variable is set and has a value other than NULL. The function returns Boolean – false if the variable is not set or true if the variable is set. The function can check multiple values: isset(var1, var2, var3…) file_uploads = On
multipart/form-data
'.<form action="sample_upload.php"method="post"enctype="multipart/form-data">​
token_get_all
function. HTTP GET
request to a URL as scheduled. The handler for that URL executes the logic when it is called. A cron job request is subject to the same limits as those for push task queues.../
’ is known as dot-dot-sequences. It is a cross-platform symbol to go up in the directory. To operate the web application file, Path Traversal makes use of the dot-dot-slash sequences.<?php
class Sample {
private $name;
private $link;
public function __construct($name) {
$this->name = $name;
}
public function setLink(Sample $link){
$this->link = $link;
}
public function __destruct() {
echo 'Destroying: '. $this->name;
}
}
?>
@
is used to suppress error messages.When we add @
before any statement in php then if any runtime error will occur on that line, then the error handled by PHP <?php
// server should keep session data
for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);
// each client should remember their
session id for EXACTLY 1 hour
session_set_cookie_params(3600);
?>
# define namespace and class in namespace
namespace Modules\Admin\;
class CityController {
}
# include the class using namesapce
use Modules\Admin\CityController ;​
__
names are magical functions/methods. Magical methods always lives in a PHP class.The definition of magical function are defined by programmer itself.__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo()
.<?php
function sendEmail (Email $email)
{
$email->send();
}
?>
$url='https://www.freetimelearning.com/get_details';
$jsonData='{"name":"phpScots",
"email":"phpscots@freetimelearning.com"
,'age':28
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_close($ch);
func_get_args()
function is used to get number of arguments passed in a PHP function.function foo() {
return func_get_args();
}
echo foo(1,5,7,3);//output 4;
echo foo(a,b);//output 2;
echo foo();//output 0;
unlink()
function is used when you want to delete the files completely. The unset()
Function is used when you want to make that file empty.unlink()
function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure. The unlink()
function in PHP accepts two-parameter.unlink(‘path to file’);
Unset()
function is an inbuilt function in PHP which is used to remove the content from the file by emptying it. It means that the function clears the content of a file rather deleting it. The unset()
function not only clears the file content but also is used to unset a variable, thereby making it empty.unset($var);
<?Php
$date1 = date('Y-m-d');
$date2 = '2017-11-21';
$days = (strtotime($date1)-strtotime($date2))/(60*60*24);
echo $days;
?>
1063
MCrypt
is a file encryption function and that is delivered as Perl extension. It is the replacement of the old crypt()
package and crypt(1)
command of Unix. It allows developers to encrypt files or data streams without making severe changes to their code.$_SERVER['REMOTE_ADDR']
to get IP address of user/client in PHP, But sometime it may not return the true IP address of the client at all time. Use Below code to get true IP address of user.function getTrueIpAddr(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
<?php
trait TraitName {
// some code...
}
?>
interface Isbn {
public function setISBN($isbn);
}
interface Type{
public function setType($type);
}
class bookDetails implements Isbn, Type {
private $isbn;
private $type;
public function setISBN($isbn)
{
$this -> isbn = $isbn;
}
public function setType($type)
{
$this -> type = $type;
}
}
imagetypes()
gives the image format and types supported by the current version of GD-PHP. __sleep
returns the array of all the variables that need to be saved, while __wakeup
retrieves them. $_ENV
is an associative array of variables sent to the current PHP script via the environment method. //Loop will iterate for 5 times
for ($n = 0; $n <= 5; $n++) {
echo "The number is: $n <br>";
}
//Loop will iterate based on array elements
$parts = array("HDD", "Monitor", "Mouse", "Keyboard");
foreach ($parts as $value) {
echo "$value <br>";
}
Start button->Command Prompt
.C:\PHP, and your script is in C:\PHP\sample-php-script.php
,C:\PHP\php.exe C:\PHP\sample-php-script.php
explode()
and split()
functions are used in PHP to split the strings. Both are defined here:Split()
is used to split a string into an array using a regular expression whereas explode()
is used to split the string by string using the delimiter.split(":", "this:is:a:split"); //returns an array that contains this, is, a, split.
Array ([0] => this,[1] => is,[2] => a,[3] => split)
explode ("take", "take a explode example "); //returns an array which have value "a explode example"
array([0] => "a explode example")
<?php
$info = array('red', 'sign', 'danger');
// Listing all the variables
list($color, $symbol, $fear) = $info;
echo "$color is $symbol of $fear”
?>​
getimagesize()
function is used to get the size of an image in PHP. This function takes the path of file with name as an argument and returns the dimensions of an image with the file type and height/width. array getimagesize( $filename, $image_info )
$variable
and special character while nowdoc doesn't do the same." "
while nowdoc string uses single quote ' '
MD5
and SHA256
are used as hashing algorithms. They take an input file and generate an output which can be of 256/128-bit
size. This output represents a checksum or hash value. As, collisions are very rare between hash values, so no encryption takes place.MySQL
database, the mysqli_connect()
function is used. It is used in the following way :<?php
$database = mysqli_connect("HOST", "USER_NAME", "PASSWORD");
mysqli_select_db($database,"DATABASE_NAME");
?>
mysqli_pconnect()
function is used for making a persistent connection with the database that does not terminate when the script ends.mysqli_connect()
function searches any existing persistence connection first and if no persistence connection exists, then it will create a new database connection and terminate the connection at the end of the script.$DBconnection = mysqli_connect("localhost","username","password","dbname");
// Check for valid connection
if (mysqli_connect_errno())
{
echo "Unable to connect with MySQL: " . mysqli_connect_error();
}
mysqli_connect
with the prefix p.fopen()
function to read or write or for doing both in PHP.$file1 = fopen("myfile1.txt","r"); //Open for reading
$file2 = fopen("myfile2.txt","w"); //Open for writing
$file3 = fopen("myfile3.txt","r+"); //Open for reading and writing
in_array()
function is used to search a particular value in an array.$languages = array("HTML", "CSS", "ANGULAR", "C", "Java", "PHP", "VB.Net");
if (in_array("PHP", $languages)) {
echo "PHP is in the list";
}
else {
echo "php is not in the list";
}
mysqli_real_escape_string()
function is used to escape special characters from the string for using a SQL statement$DBconnection=mysqli_connect("localhost","username","password","dbname");
$productName = mysqli_real_escape_string($con, $_POST['proname']);
$ProductType = mysqli_real_escape_string($con, $_POST['protype']);
$str = " Tutorials for your help";
$val1 = trim($str);
$val2 = ltrim($str);
$val3 = rtrim($str);​
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
-_
. have replece with a percentege(%) sign . the urldecode->Decodes
url to encode string as any %
and other symbole are decode by the use of the urldecode()
function. <?php
if ($_FILES["file"]["error"] == 0)
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
?>