Most Useful PHP Functions with Examples (Part-1)

Table of Contents

unset(): This function is used to clear the value of the variable. even “null” is used to do the same work.
Example

<?php

$name=”something”;

unset($name);

?>

 

var_dump(): This function is used to get the X-ray of the variable or the integer.

Example

<?php

// define variables

$name = ‘Fiona’;

$age = 28;

 

// display variable contents

var_dump($name);

var_dump($age);

?>

 

gettype(): This function is used to get the type of the variable. Means it checks whether it is string, integer, boolean or null etc.

Example

<?php

$name=”techgyo”;

echo gettype($name);

?>

 

define(): This function is used to define constant data type. The values of the constants never changes it accepts two arguments: the name of the constant, and its value.  $ prefix is not required for constant names.

Example

<?php

// define constants

define (‘PROGRAM’, ‘The Matrix’);

define (‘VERSION’, 11.7);

 

// use constants

// output: ‘Welcome to The Matrix (version 11.7)’

echo ‘Welcome to ‘ . PROGRAM . ‘ (version ‘ . VERSION . ‘)’;

?>

. (dot) is used to concatenate the strings or integers etc.

Example

<?php

$name=”techgyo”;

$name2=”.com”;

echo $name . $name2; //Result will be techgyo.com

?>

 

isset () Determine whether the variable is set. It is very useful when we want to know whether the form is submitted or not.

Example

<?php

//Get the value

$txtname = $_POST[‘txt’];

 

//Process

if (!isset($_POST[‘button’])) {

//Form is not yet submitted

} else {

//Form is submitted

}

?>

 

empty () Checks whether the string is empty or not.

Example

<?php

$str=””;

echo (boolean) empty ($str);

?>

 

strlen () Returns the number of characters in the string.

Example

<?php

$str=”techgyo”;

echo strlen($str);

?>

 

strrev () This function is used to reverse the string.

Example

<?php

$str=”PHP is nice language”;

echo strrev($str);

?>

 

str_repeat () This function is used to repeat the string. It accepts two arguments—the string to be repeated, and the number of times to repeat it.

Example

<?php

$str=”PHP is nice language”;

echo str_repeat($str,5);

?>

 

substr () This function has 3 arguments, The original string, position to start and number of characters to return. (We can use “-” (negative) to get the strings from the end).

Example

<?php

$str=”PHP is nice language”;

echo substr($str,1,2); // Result is “HP”

?>

 

strcmp () This function is used to compare strings (First string with second string), it is case sensitive. Returns “0” if both are same and returns 1 if strings don’t match each other

Example

<?php

$a=”hello”;

$b=”hello”;

$c=”Hello”;

 

echo strcmp($a,$c)

?>

 

str_word_count () This function is used to get the number of words in a string.

Example

<?php

$a=”hello I am PHP”;

echo str_word_count($a); //Result is 4

?>

 

str_replace () This function is used to find and replace the words in the strings.  This function accepts three arguments: the search term, the replacement term, and the string in which to perform the replacement.

Example

<?php

$a=”hello I am PHP”;

echo str_replace (“hello”, “hi”, $a); //Result is “hi I am PHP”

?>

 

trim () This function is used to trim the string.

Example

<?php

$a=”   hello I am PHP  “;

echo trim ($a); //Result is “hello I am PHP”

?>

 

strlower () This function is used to make the string lowercase.

strupper () This function is used to make the string uppercase.

ucfirst () (Uppercase First) This function is used to make the string’s first letter uppercase.

ucword () (Uppercase Word) This function is used to make the string’s words  uppercase.

 

addslashes () This function is used to escape the special characters.

Example

<?php

// escape special characters

// output: ‘You\’re awake, aren\’t you?’

$str = “You’re awake, aren’t you?”;

echo addslashes($str);

?>

 

floor () This function is used to round down the number.

Example

<?php

$num = 19.7;

echo floor($num); //Result is 19

?>

 

ceil () This function is used to round up the number.

Example

<?php

$num = 19.7;

echo ceil($num); //Result is 20

?>

 

abs () This function is used to make the number absolute means (-) negative number becomes (+) positive.

Example

<?php

$num = -19.7;

echo abs($num); //Result is 19.7

?>

 

rand () This function is used to randomize the number. It accepts two arguments, we have to specify 2 numbers to get the random number between those numbers.

Example

<?php

echo rand(10,100);

?>


Most Useful PHP Functions with Examples (Part-1) 1
Shrinivashttps://techgyo.com
Hi, this is +Shrinivas, from Belgaum. I'm a part of the Techgyo community.

Grab your lifetime license to AI Image Generator. Hostinger Hosting

X