In PHP, the isset() method is used for checking if one or more variables have been declared and are not null. $variable = "I'm a variable"; if (isset($variable)) { echo "variable has been declared!"; } else { echo "variable hasn't been declared!" } if (isset($variable, $another_variable)) { echo "all variables have been declared!"; } else […]
PHP
PHP random_int() Function – Generate Random Integers in PHP
To generate a random integer using php, you can use the php random_int() function. echo random_int(0,100); // Output: 54 Random number generation with php is easy to do. If you want to generate random integers, you can use rand(), but if you want to generate cryptographically secure integers, then you can use the php random_int() […]
PHP Variables Are Preceded by $
In PHP, when you create a new variable or go to use an existing variable, you need to put a dollar sign $ before the variable name. $a = 1 $b = 2 $c = $a + $b Depending on which programming language you use, there can be different conventions around how you can define […]
PHP Numerically Indexed Array Begin With Position 0
In php, numerically indexed arrays begin with position 0 because php is a zero-based programming language. $array = array(101,202,303); print_r($array); // Output: Array ( [0] => 101 [1] => 202 [2] => 303 ) Depending on which programming language you use, there are sometimes different conventions and behaviors which you need to know. One such […]
Get Last Day of Month in php
To get the last day of a month, you can use the php date() function and format with “Y-m-t”. “t” returns the last day of a month. echo date('Y-m-t'); echo date('Y-m-t', strtotime('22-04-01')); // Output: 2022-04-30 2022-04-30 When working with date objects in php, the ability to easily change or get information about a specific time […]
Get Days in Month Using php
To get the days in a month, you can use the php cal_days_in_month() function. echo cal_days_in_month(CAL_GREGORIAN,4,2022); // Output: 30 You can also pass ‘t’ to date() to get the number of days in the current month. echo date('t'); // Output: 30 When working with dates in php, the ability to easily be able to get […]
Add Months to Date in php
To add months to a date in php, you can use the strtotime() function as shown below. $today = "2022-04-27"; $one_month_in_future = strtotime($today . "+1 month"); echo date('Y-m-d', $one_month_in_future); //Output: 2022-05-27 You can also use the php date_add() function. $today= date_create("2022-04-27"); $one_month_in_future = date_add($today, date_interval_create_from_date_string("1 month")); echo date_format($one_month_in_future, "Y-m-d"); // Output: 2022-05-27 When working with […]
Get Current Month of Date in php
To get the name of the current month in php, you can use the php date() function and pass “F”. $month_name = date("F"); echo $month_name; // Output: April If you want to get the current month number, you can use the php idate() function and pass ‘m’. $month_number = idate('m'); echo $month_number ; // Output: […]
php array_walk() – Modify Elements in Array with Callback Function
The php array_walk() function gives us the ability to apply a function to all elements in an array. array_walk() modifies the passed array if the function parameter is passed by reference function square(&$var) { $var = $var * $var; } $array = array(1,2,3,4,5); array_walk($array, "square"); print_r($array); // Output: Array ( [0] => 1 [1] => […]
php array_map() – Create New Array from Callback Function
The php array_map() function gives us the ability to create a new array by applying a function to all elements of one or more arrays. function square($var) { return $var * $var; } $array = array(1,2,3,4,5); print_r(array_map("square", $array)); // Output: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] […]
php array_slice() – Get Slice of Elements from Array
The php array_slice() function gives us the ability to slice an array and extract elements at specific positions. $array = array('bear', 'elephant', 'rabbit', 'salmon', 'alligator', 'whale'); // Slice array from position 1 to 3 print_r(array_slice($array, 1, 2)); // Output: Array ( [0] => elephant [1] => rabbit ) When working with collections of data in […]
php array_splice() – Remove, Insert, and Replace Elements in Array
The php array_splice() function gives us the ability to remove elements from an array by their position, insert new elements at a specific position, and replace elements. To remove an element from an array, pass an array and the position of the element you want to remove to array_splice() $array = array('bear', 'elephant', 'rabbit', 'salmon', […]
php array_filter() – Filter Array Elements with Function in php
The php array_filter() function gives us the ability to filter an array with a callback function. You can filter both values and keys with array_filter() function greater_than_5($var) { return $var > 5; } $array = array(1,2,3,4,5,6,7,8,9,10); print_r(array_filter($array, "greater_than_5")); // Output: Array ( [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] […]
php array_key_exists() Function – Check if Key Exists in Array
The php array_key_exists() function checks an array to see if a given key exists in that array. $array = array("black" => "bear", "grey" => "elephant", "yellow" => "lion"); var_dump(array_key_exists("black",$array)); // Output: bool(true) When working with arrays in php, it can be valuable to be able to easily gather information of the properties of an array. […]
Get Word Count of String in php with str_word_count() Function
In php, we can get the word count and number of words in a string with the str_word_count() function. $string = "This is a string"; echo str_word_count($string); // Output: 4 When working with string variables in our code, the ability to easily get information about the variables is valuable. One such piece of information is […]
php sleep() Function – Pause Execution of Code in php
The php sleep() function gives us the ability to pause the execution of our code. sleep() delays the execution of code by a given number of seconds. sleep(30); // program sleeps for 30 seconds When programming in php, sometimes we want to delay the execution of a piece of code so that other processes can […]
php is_float() Function – Check if Variable is Float in php
The php is_float() function checks if a variable is a float or not. If the given variable is a float, is_float() returns TRUE. Otherwise, is_float() returns FALSE. $float = 1.0; $array = array(1,2,3); $string = "This is a string"; var_dump(is_float($float)); var_dump(is_float($array)); var_dump(is_float($string)); // Output: bool(true) bool(false) bool(false) When working with different variables in your php […]
php is_string() Function – Check if Variable is String in php
The php is_string() function checks if a variable is an string or not. If the given variable is an string, is_string() returns TRUE. Otherwise, is_string() returns FALSE. $string = "This is a string"; $array = array(1,2,3); $float = 1.0; var_dump(is_string($string)); var_dump(is_string($array)); var_dump(is_string($float)); // Output: bool(true) bool(false) bool(false) When working with different variables in your php […]
php is_array() Function – Check if Variable is Array in php
The php is_array() function checks if a variable is an array or not. If the given variable is an array, is_array() returns TRUE. Otherwise, is_array() returns FALSE. $array = array(1,2,3); $float = 1.0; $string = "This is a string"; var_dump(is_array($array)); var_dump(is_array($float)); var_dump(is_array($string)); // Output: bool(true) bool(false) bool(false) When working with different variables in your php […]
php array_diff() – Find Elements of Array Not Present in Other Arrays
The php array_diff() function allows us to find the elements of an array which are not in all of the other given arrays. $array1 = array(5,3,8,2,1); $array2 = array(9,3,4,2,1); print_r(array_diff($array1,$array2)); // Output: Array ( [0] => 5 [2] => 8 ) When working with multiple arrays, it can be useful to find the entire collection […]
php array_intersect() – Find Intersection of Two or More Arrays in php
The php array_intersect() function allows us to find the intersection (common elements) of two or more arrays in php. $array1 = array(5,3,8,2,1); $array2 = array(9,3,4,2,1); print_r(array_intersect($array1,$array2)); // Output: Array ( [1] => 3 [3] => 2 [4] => 1 ) When working with multiple arrays, it can be useful to find the entire collection of […]
php array_fill() – Create Array of Length N with Same Value
The php array_fill() function fills an array with n entries of the same given value with keys starting from given starting index. $array = array_fill(0, 5, "item"); print_r($array); // Output: Array ( [0] => item [1] => item [2] => item [3] => item [4] => item ) When working with collections of data in […]
php array_merge() – Merge One or More Arrays Together by Appending
The php array_merge() function allows us to append the items of one or more arrays to the items of a given array. $array1 = array("bear","elephant","lion"); $array2 = array(1,2,3); $array3 = array("orange","red","yellow"); print_r(array_merge($array1,$array2,$array3)); // Output: Array ( [0] => bear [1] => elephant [2] => lion [3] => 1 [4] => 2 [5] => 3 [6] […]
How to Get Current System IP Address in php
To get the current system IP address in php, we can access the ‘REMOTE_ADDR’ key from the $_SERVER super global variable. $ip_address = $_SERVER['REMOTE_ADDR']; If the client is using a proxy or accessing your page with shared internet, you use the ‘HTTP_X_FORWARDED_FOR’ and ‘HTTP_CLIENT_IP’ keys respectively. $ip_address_proxy = $_SERVER['HTTP_X_FORWARDED_FOR']; $ip_address_shared_internet = $_SERVER['HTTP_CLIENT_IP']; If you are […]
Get Current Location Latitude Longitude in php
In php, we can get the current location’s latitude and longitude from an address and also get an address from a latitude and longitude by leveraging the Google Maps API. To get the latitude and longitude from an address, you can define a custom php function as shown below. function getLatLong($address){ $formatted_address = str_replace(' ','+',$address); […]
How to Parse URLs with php parse_url() Function
To parse a URL in php, you can use the php parse_url() function and get an array with the URL scheme, host, path, query string, and other information. $url = "http://theprogrammingexpert.com/php-parse-url?user=friend#hello"; print_r(parse_url($url)); //Output: Array ( [scheme] => https [host] => theprogrammingexpert.com [path] => /php-parse-url [query] => user=friend [fragment] => hello ) If you want to […]
Remove HTML Tags from String in php
To remove all HTML tags from a string in php, the easiest way is to use the strip_tags() function. $string_with_html_tags = "<p>I'm a string with <span>HTML</span>!</p>"; echo strip_tags($string_with_html_tags); //Output: I'm a string with HTML! Keeping Certain HTML Tags and Stripping All Other Tags with strip_tags() in php There is a second optional parameter for the […]
php array_flip() – Flip the Keys and Values of an Array in php
The php array_flip() function interchanges the keys and values of an array, making a new array with keys which are the given array’s values and values which are the given array’s keys. $array = array('bear', 'elephant', 'rabbit'); $flipped_array = array_flip($array1); print_r($array); print_r($flipped_array); // Output: Array ( [0] => bear [1] => elephant [2] => rabbit […]
What is the Difference Between unset() and unlink() in php?
unset() and unlink() are two very useful php functions. The difference between unset() and unlink() in php is unset() deletes variables and unlink() deletes files. $variable = "I'm a variable"; unset($variable); unlink("./folder/file_name1.txt"); In php, there are many different functions which allow us to create complex programs. Some of the functions have similar names which make […]
php array_combine() – Create New Array Given Arrays of Keys and Values
The php array_combine() function creates a new array given two arrays, where one array is used for the keys and the other array is used for the values of the new array. $array1 = array('bear', 'elephant', 'rabbit'); $array2 = array(1,2,3); $new_array = array_combine($array1, $array2); print_r($new_array); // Output: Array ( [bear] => 1 [elephant] => 2 […]
Delete Cookie Using php
To delete a cookie in php, you can use the php setcookie() function with an expiration time which is in the past. It is also good practice to unset the variable with unset() from the $_COOKIE super global variable. setcookie("cookie", "", time() – 3600); unset($_COOKIE["cookie"]); When working with web pages in php, many times we […]
Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
To get the user agent in php, you can use the $_SERVER super global variable and access the key ‘HTTP_USER_AGENT’. $user_agent = $_SERVER['HTTP_USER_AGENT']; The User-Agent request header is a string that lets servers and network peers identify various information like the application, operating system, vendor, and/or version of the requesting user agent. When building web […]
Get Query String from URL in php
To get the query string from a URL in php, you can use $_GET super global to get specific key value pairs or $_SERVER super global to get the entire string. // http://theprogrammingexpert.com/?key1=value1&key2=value2&key3=value3 echo $_GET['key1']; echo $_GET['key2']; echo $_SERVER['QUERY_STRING']; //Output: value1 value2 key1=value1&key2=value2&key3=value3 A query string is a part of a URL that assigns values […]
php session_destroy() – Delete Session File Where Session Data is Stored
When working with sessions in php, you can use the session_destroy() function to destroy all data associated with a current active session by deleting the session file. The session variables will still be available, but will not be connected with the session. session_start(); // do things with your session session_destroy(); If you want to kill […]
How to Get Session ID in php with session_id()
To get the session ID in php, you can use the php session_id() function. session_id() returns the session ID if a session is active. session_start(); $session_id = session_id(); If you want to start a session with your own session ID, then you can use session_id() before you start a session with your own ID. session_id($your_id); […]
php json_encode() Function – Get JSON Representation of Variables in php
The php json_encode() function allows us to get the JSON representation of variables. class exampleClass { public $id; private $name; static protected $multiplyNumbers; static function multiplyNumbers($a,$b) { return $a * $b; } } $num = 5; $string = "This is a string"; $array = ["This","is","an","array"]; echo json_encode(new exampleClass) . "\n"; echo json_encode($num). "\n"; echo json_encode($string). […]
php var_dump() Function – Print Information about Variable Type and Value
The php var_dump() function allows us to print information about the type and value of a variable. $num = 5; $string = "This is a string; $array = ["This","is","an","array"]; var_dump($num, $string, $array); //Output: int(5) string(16) "This is a string" array(4) { [0]=> string(4) "This" [1]=> string(2) "is" [2]=> string(2) "an" [3]=> string(5) "array" } When […]
php print_r() Function- Get Information about Variables in php
The php print_r() function allows us to get human-readable information about our php variables. We can print arrays and objects with print_r(). $num = 5; $string = "This is a string"; $array = ["This","is","an","array"]; print_r($num); print_r($string); print_r($array); //Output: 5 This is a string Array ( [0] => This [1] => is [2] => an [3] […]
php Print Array – How to Print All Elements in Array
When working in php, we can print an array easily. To print arrays, you can use loops, print_r(), var_dump(), or json_encode(). The first example here shows how to print all the elements in an array with a loop. $array = ["This","is","an","array"]; foreach($array as $ele) { echo $ele . "\n"; } //Output: This is an array […]
php switch case – How to Use Switch…Case Statements in php
In php, a switch case statement tests a variable against multiple values and when the switch statement finds a match, it executes code in the case block. $variable = "apple"; switch($variable) { case "apple": echo "The variable is apple."; break; case "banana": echo "The variable is banana."; break; default: echo "The variable is something else."; […]
php range() – Create Array of Elements Within Given Range
With the php range() function, we can create arrays of elements in a range of numbers and letters. $arr = range(0,10); print_r($arr); //Output: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 […]
php str_split() – Convert String into Array of Equal Length Substrings
The php str_split() function allows us to convert a string into an array of equal length substrings. By default, str_split() splits a string into an array of the string’s characters. $variable = "I'm a variable"; print_r(str_split($variable)); //Output: Array ( [0] => I [1] => ' [2] => m [3] => [4] => a [5] => […]
php strlen() – Get the Length of String Variable in php
In php, the strlen() function allows us to get number of characters and length of a string variable. $variable = "I'm a variable"; echo strlen($variable); //Output: 14 When working with string variables in our php programs, it is useful to be able to easily extract information about the values of the strings. In php, we […]
Get Array Length with php count() Function
In php, we can get the length of an array easiest with the php count() function. $array = array(1, 2, 3, 4, 5); echo count($array); // Output: 5 When working with arrays in php, it can be valuable to be able to easily gather information of the properties of an array. One such situation is […]
php array_shift() – Remove the First Element from Array
The php array_shift() function allows us to get and delete the first element of an array in php. $array = array(1, 2, 3, 4, 5); array_shift($array); print_r($array); // Output: Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 ) When working with arrays and collections of data in php, it […]
php array_pop() – Remove Last Element from Array
The php array_pop() function allows us to get and delete the last element of an array in php. $array = array(1, 2, 3, 4, 5); array_pop($array); print_r($array); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) When working with arrays and collections of data in php, it […]
Truncate Strings in php Using substr()
To truncate a string variable in php, the easiest way is to use the php substr() function. $variable = "this is a variable"; $truncated = substr($variable,0,6); echo $truncated; //Output: this i Another way to truncate a string is with the mb_strimwidth() function. $variable = "this is a variable"; $truncated = mb_strimwidth($variable,0,6); echo $truncated; //Output: this […]
Remove Duplicates from Array in php with array_unique()
In php, to remove duplicates from an array, the easiest way is to use the array_unique() function to get the unique values and then use array_values() to reindex the array. $example = array(1,2,4,7,5,4,1,2,3,3,3,4,4,5,5,5,6); print_r(array_values(array_unique($example))); //Output: Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 7 [4] => 5 [5] => 3 […]
How to Reverse Array in php Without a Function or Other Variables
To reverse an array in php without a function, we can use a loop and swap items in order. $example = array("lion", "bear", "snake", "horse"); foreach(range(0,count($example)/2) as $i) { [$example[$i], $example[count($example) – $i – 1]] = [$example[count($example) – $i – 1],$example[$i]]; } print_r($example); //Output: Array ( [0] => horse [1] => snake [2] => bear […]
Reversing an Array in php with array_reverse() Function
In php, you can reverse the items in an array easiest with the array_reverse() function. $example = array("lion", "bear", "snake", "horse"); $reversed = array_reverse($example); print_r($reversed); //Output: Array ( [0] => horse [1] => snake [2] => bear [3] => lion ) When working with arrays and collections of data in php, it is useful to […]
How to Add Items to Array in php
In php, we can add items to arrays easily. If you want to add items to the end of an array, you can use the php array_push() function. $example = array("lion"); array_push($example, "bear", "snake", "horse"); print_r($example); //Output: Array ( [0] => lion [1] => bear [2] => snake [3] => horse ) If you want […]
How to Create an Empty Array in php
In php, there are three ways you can create an empty array. Creating an empty array can be useful if your data is dynamic. $emptyArray = []; $emptyArray = array(); $emptyArray = (array) null When working with arrays and collections of data in php, it is useful to be able to create and use different […]
php rtrim() Function – Remove Whitespace at End of String
In php, the rtrim() function is very useful if we have strings with unwanted whitespace. With the php rtrim() function, we can remove whitespace from the end of a string. $variable = " this is a variable "; echo rtrim($variable); //Output: this is a variable When working with string variables in our php programs, it […]
php trim() Function – Remove Whitespace at Beginning and End of String
In php, the trim() function is very useful if we have strings with unwanted whitespace. With the php trim() function, we can remove whitespace from the beginning and end of a string. $variable = " this is a variable "; echo trim($variable); //Output: this is a string variable When working with string variables in our […]
Capitalize First Letter of String with php ucfirst() Function
In php, we can capitalize the first letter of a string easily with the php ucfirst() function. $variable = "this is a string variable"; echo ucfirst($variable); //Output: This is a string variable When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of […]
Using strtolower() in php to Convert String to Lowercase
In php, we can make all characters in a string lowercase easily with the php strtolower() function. $variable = "THIS IS A STRING VARIABLE"; echo strtolower($variable); //Output: this is a string variable When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of […]
Using strtoupper() in php to Convert String to Uppercase
In php, we can make all characters in a string uppercase easily with the php strtoupper() function. $variable = "this is a string variable"; echo strtoupper($variable); //Output: THIS IS A STRING VARIABLE When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of […]
Check if String Ends with Given String in php with str_ends_with()
To check if a string ends with a particular string in php, for users of php 8.0.0 or later, the easiest way is to use the php str_ends_with() function. $variable = "I'm a variable"; echo var_dump(str_ends_with($variable, "I'm")); echo var_dump(str_ends_with($variable, "a variable")); echo var_dump(str_ends_with($variable, "variable")); //Output: bool(false) bool(true) bool(true) If you are using a version of […]
Check if String Starts with Given String in php with str_starts_with()
To check if a string starts with a particular string in php, for users of php 8.0.0 or later, the easiest way is to use the php str_starts_with() function. $variable = "I'm a variable"; echo var_dump(str_starts_with($variable, "I'm")); echo var_dump(str_starts_with($variable, "I'm a")); echo var_dump(str_starts_with($variable, "variable")); //Output: bool(true) bool(true) bool(false) If you are using a version of […]
php unset – How to Destroy Variables in php
The php unset() function destroys one or more variables. We can use unset() to delete previously declared variables. $variable = "I'm a variable"; echo var_dump(isset($variable)); unset($variable); echo var_dump(isset($variable)); //Output: bool(true) bool(false) When working with variables in our php programs, it is useful to be able to delete a variable. In php, the unset() function allows […]
Using function_exists() to Check if Function Exists in php
In php, we can check if a function exists with the php function_exists() function. if (function_exists('max')) { echo "max() exists!"; } else { echo "max() doesn't exist!"; } if (function_exists('other_function')) { echo "other_function() exists!"; } else { echo "other_function() doesn't exist!"; } //Output: max() exists! other_function() doesn't exist! When working in php, it is useful […]
Using isset() Function in php to Check if Variable is Defined
The isset() function in php allows us to check if one or more variables have been declared to and is not null. $variable = "I'm a variable"; if (isset($variable)) { echo "variable has been declared!"; } else { echo "variable hasn't been declared!" } if (isset($variable, $another_variable)) { echo "all variables have been declared!"; } […]
Check if Variable Exists in php with isset() Function
In php, we can check if a variable exists with the php isset() function. $variable = "I'm a variable"; if (isset($variable)) { echo "variable exists!"; } else { echo "variable doesn't exist!" } //Output: variable exists! When working with variables in our php programs, it is useful to be able to check if a variable […]
php property_exists() – Check if Property Exists in Class or Object
In php, we can check if a property exists in a class or an object with the built-in php property_exists() function. class exampleClass { public $id; private $name; static protected $multiplyNumbers; static function multiplyNumbers($a,$b) { return $a * $b; } } var_dump(property_exists('exampleClass', 'name')); var_dump(property_exists(new exampleClass, 'name')); var_dump(property_exists('exampleClass', 'multiplyNumbers')); var_dump(property_exists('exampleClass', 'other')); // Output: bool(true) bool(true) bool(true) […]
Remove String from String in php with str_replace()
To remove a string from another string variable in php, the easiest way is to use the php str_replace() function. $string = "This is a string."; $string_without_his = str_replace("his","", $string); echo $string_without_his; // Output: T is a string. You can also use the preg_replace() function to use regex to remove strings from string variables in […]
Remove Specific Character from String Variable in php
To remove a specific character from a string variable in php, the easiest way is to use the php str_replace() function. $string = "This is a string."; $string_without_i = str_replace("i","", $string); echo $string_without_i; // Output: Ths s a strng. You can also use the preg_replace() function to use regex to remove characters from a string […]
Replace Underscore with Space in php String
To replace a space with a dash in php, the easiest way is to use the php str_replace() function. $string_with_underscores = "This_is_a_string."; $string_with_spaces = str_replace("_"," ", $string_with_underscores); echo $string_with_spaces; // Output: This is a string. You can also use the preg_replace() function to use regex to replace underscores with spaces in php. $string_with_underscores = "This_is_a_string."; […]
Replace Space with Dash in php String
To replace a space with a dash in php, the easiest way is to use the php str_replace() function. $string_with_spaces = "This is a string."; $string_with_dashes = str_replace(" ","-", $string_with_spaces); echo $string_with_dashes; // Output: This-is-a-string. You can also use the preg_replace() function to use regex to replace spaces with dashes in php. $string_with_spaces = "This […]
Get Last Element of Array in php
To get the last element of an array in php, the easiest way is with the end() function. $array = array(1, 2, 3, 4, 5); echo end($array); // Output: 5 Another way to get the last element of an array is with the array_slice() function. $array = array(1, 2, 3, 4, 5); echo array_slice($array, -1)[0]; […]
Get First Element of Array in php
To get the first element of an array in php, the easiest way is by accessing the first element directly. $array = array(1, 2, 3, 4, 5); echo $array[0]; // Output: 1 Another way to get the first element of an array is with the current() function. $array = array(1, 2, 3, 4, 5); echo […]
Remove First Character from String Using php
To remove the first character from a string in php, the easiest way is with php substr() function. $string = "This is a string variable"; $string_without_first_char = substr($string,1); echo $string_without_first_char; // Output: his is a string variable You can also use the php ltrim() function. $string = "This is a string variable"; $string_without_first_char = ltrim($string,$string[0]); […]
Remove Last Character from String Using php
To remove the last character from a string in php, the easiest way is with php substr() function. $string = "This is a string variable"; $string_without_last_char = substr($string,0,-1); echo $string_without_last_char; // Output: This is a string variabl You can also use the php rtrim() function. $string = "This is a string variable"; $string_without_last_char = rtrim($string,$string[-1]); […]
php e – Using M_E and exp() Function to Get Euler’s Constant e
To get the value of Euler’s Constant e in php, the easiest way is to use the php constant M_E. M_E returns the value 2.718281828459. echo M_E; // 2.718281828459 You can also use the php exp() function. exp(1) returns the value 2.718281828459. echo exp(1); // 2.718281828459 In php, we can easily get the value of […]
php Trig – Using Trigonometric Functions in php for Trigonometry
In php, we can easily do trigonometry with the many trig functions from the php math functions. In this article, you will learn about all the trigonometric functions that you can use in php to perform trigonometry easily. In php, we can easily use trigonometric functions with the help of the php math functions. The […]
php Convert Degrees to Radians with php deg2rad() Function
To convert degrees to radians for use in trigonometric functions in php, the easiest way is with the php deg2rad() function. $radians = deg2rad(60) The php collection of math functions has many powerful functions which make performing certain calculations in php very easy. One such calculation which is very easy to perform in php is […]
php Convert Radians to Degrees with php rad2deg() Function
To convert radians to degrees for use in trigonometric functions in php, the easiest way is with the php rad2deg() function. $degrees = rad2deg(pi()/2) The php collection of math functions has many powerful functions which make performing certain calculations in php very easy. One such calculation which is very easy to perform in php is […]
php pi – Get Value of pi Using php pi() Function
To get the value of pi in php, the easiest way is use the php pi() function. pi() returns the value 3.1415926535898. echo pi(); // 3.1415926535898 You can also use the php constant M_PI echo M_PI; // 3.1415926535898 In php, we can easily get the value of pi for use in trigonometry with the php […]
php atan2 – Find Arctangent of the Quotient of Two Numbers
To find the arctangent, or inverse tangent, of the quotient of two numbers, we can use the php atan2() function. atan2(x,y) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. php gives us the ability to find the arctangent […]
php atan – Find Arctangent and Inverse Tangent of Number
To find the arctangent, or inverse tangent, of a number, we can use the php atan() function. atan(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. To find the arctangent of a number, we use the php atan() […]
php tan – Find Tangent of Number in Radians Using php tan() Function
To find the tangent of a number (in radians), we can use the php tan() function. tan(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. To find the tangent of a number, in radians, we use the php […]
php cos – Find Cosine of Number in Radians Using php cos() Function
To find the cosine of a number (in radians), we can use the php cos() function. cos(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. To find the cosine of a number, in radians, we use the php […]
php acos – Find Arccosine and Inverse Cosine of Number
To find the arccosine, or inverse cosine, of a number, we can use the php acos() function. acos(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. To find the arccosine of a number, we use the php acos() […]
php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
To find the hyperbolic arctangent of a number, we can use the php atanh() function. atanh(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily and in addition to the standard trigonometry, we have access to the hyperbolic trigonometry […]
php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
To find the hyperbolic tangent of a number, we can use the php tanh() function. tanh(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily and in addition to the standard trigonometry, we have access to the hyperbolic trigonometry […]
php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
To find the hyperbolic arccosine of a number, we can use the php acosh() function. acosh(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily and in addition to the standard trigonometry, we have access to the hyperbolic trigonometry […]
php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
To find the hyperbolic cosine of a number, we can use the php cosh() function. cosh(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily and in addition to the standard trigonometry, we have access to the hyperbolic trigonometry […]
php asinh – Find Hyperbolic Arcsine of Number Using php asinh() Function
To find the hyperbolic arcsine of a number, we can use the php asinh() function. asinh(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily and in addition to the standard trigonometry, we have access to the hyperbolic trigonometry […]
php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
To find the hyperbolic sine of a number, we can use the php sinh() function. sinh(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily and in addition to the standard trigonometry, we have access to the hyperbolic trigonometry […]
php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
To find the arcsine, or inverse sine, of a number, we can use the php asin() function. asin(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. To find the arcsine of a number, we use the php asin() […]
php sin – Find Sine of Number in Radians Using php sin() Function
To find the sine of a number (in radians), we can use the php sin() function. sin(x) In php, we can easily use trigonometric functions from the collection of php math functions. These php math functions allow us to perform trigonometry easily. To find the sine of a number, in radians, we use the php […]
php floor – Round Down and Find the Floor of a Number in php
To find the floor of a number using php, the easiest way is to use the php floor() function. echo floor(2.434); // Output: 2 Finding the floor of a number in php is easy. We can round down numbers to the nearest integer with the php floor() function. echo floor(5.4); // Output: 5 We can […]
php ceil – Round Up and Find the Ceiling of a Number in php
To find the ceiling of a number using php, the easiest way is to use the php ceil() function. echo ceil(2.434); // Output: 3 Finding the ceiling of a number in php is easy. We can round up numbers to the nearest integer with the php ceil() function. echo ceil(5.4); // Output: 6 We can […]
php Delete Files with php Unlink Function
To delete a file using php, the easiest way is to use the php unlink() function. unlink("./folder/file_name1.txt"); Deleting files with php is easy. We can use the php unlink() function to delete files If we want to delete a file named “file_name.txt”, we can do this with the following php code: unlink("./folder/file_name.txt"); Deleting Multiple Files […]
Using php to Copy Files Easily from One Directory to Another
To copy a file using php, the easiest way is to use the php copy() function. copy("./folder/file_name1.txt","./folder/file_name2.txt"); Copying files with php is easy. We can use the php copy() function to copy files. If we want to copy a file named “file_name.txt” to “copied_file_name.txt”, we can do this with the following php code: copy("./folder/file_name.txt","./folder/copied_file_name.txt"); One […]
php Rename File – Renaming Files and Directories Easily
To rename a file from one name to another using php, the easiest way is to use the php rename() function. rename("./folder/file_name1.txt","./folder/file_name2.txt"); Renaming files with php is easy. We can use the php rename() function to rename files and rename directories. If we want to rename a file named “old_file_name.txt” to “new_file_name.txt”, we can do […]
php Move File from One Directory to Another Folder
To move a file from one directory to another using php, the easiest way is to use the php rename() function. rename("./folder1/file.txt","./folder2/file.txt"); Moving files with php is easy. We can use the php rename() function to move a file into a different folder or directory. If we want to move a file named “file.txt” from […]
php is_numeric – Check if Variable is a Number
To check if a variable is a number and is numeric in php, the easiest way is to use the php is_numeric() function. if (is_numeric("31")) { echo "True"; } else { echo "False"; } // Output: True if (is_numeric(1.3)) { echo "True"; } else { echo "False"; } // Output: True if (is_numeric("php")) { echo […]
php Random Number Generator with rand Function
To generate a random number using php, the simplest way is to use the php rand() function: echo rand(); // Output: 1123 echo rand(0,10); // Output: 3 Random number generation with php is easy to do. All we need is the php rand() method. The php rand() method generates random integers. There are two ways […]
php getimagesize – Get Height, Width and Image File Type
To get the image height, image width, and image file type in php, we can use the php getimagesize() function. $image_information = getimagesize("sample_image.png"); print_r($image_information); // Output: Array ( [0] => 500 [1] => 250 [2] => 3 [3] => width="500" height="250" [bits] => 8 [mime] => image/png ) Many times when designing web pages, we […]
php Square Root with sqrt Function
To find the square root of a number in php, the easiest way is with the php sqrt() function. echo sqrt(4); // Output: 2 Finding the square root of a number in php is easy. We can find the square root of both integers and floats using the php sqrt() function. In mathematics, the square […]
php Absolute Value with abs Function
To find the absolute value of a number in php, the easiest way is with the php abs() function. echo abs(-4); // Output: 4 Finding the absolute value of a number in php is easy. We can find the absolute number of both integers and floats using the php abs() function. The absolute value of […]
php preg_match_all – Get All Matches of Pattern in String
You can use the php preg_match_all() function to perform a regular expression (regex) search on a string, and return all matches of the regex pattern found. $sample_string = "This is a string with some words and 1 number."; $pattern = '/(?<!\S)\S{1}(?!\S)/'; // Searching for all 1 letter words preg_match_all($pattern,$sample_string, $matches); print_r($matches); Array ( [0] => […]
php preg_match – Find Pattern in String Using Regex (Regular Expression)
You can use the php preg_match() function to perform a regular expression (regex) search on a string, and return if a match of the regex pattern found, and find out what the first match is. $sample_string = "This is a string with some words."; $pattern = '/(?<!\S)\S{1}(?!\S)/'; // Searching for all 1 letter words echo […]
preg_replace php – Use Regex to Search and Replace
You can use the php preg_replace() function to perform a regular expression (regex) search on a string or array of strings, and returns a string or an array of strings where all matches of the regex pattern or list of regex patterns found are replaced with substrings. $old_string = "This is a string with too […]
echo php – Output One or More Strings
php echo is one of the most used constructs in php. The echo is not a function, but a language construct and allows us as php programmers to output one or more strings. echo "Hello World!"; // Output: Hello World! The php echo statement is one of the most used and most useful statements in […]
php str_replace – Replace String in php
You can use the php str_replace() function to replace strings in a string or an array of strings with a replacement string in the following way: $old_string = "This is the old string"; $new_string = str_replace("old","new",$old_string); echo $new_string; // "This is the new string" From the php documentation, the php str_replace() function takes 4 parameters: […]
php max – Find Maximum Value of Array in php
To find the maximum value of an array in php, we can use the php max() function. $max = max($values) The max function returns the “highest” value according to standard comparisons in php. If there are multiple values which have the same value, then the first one will be returned. To get the maximum of […]
php min – Find Minimum Value of Array in php
To find the minimum value of an array in php, we can use the php min() function. $min = min($values) The min function returns the “lowest” value according to standard comparisons in php. If there are multiple values which have the same value, then the first one will be returned. To get the minimum of […]
php in_array – Check If Value is in php Array
The php in_array() function allows us to check if a value is in a php array. The php in_array() function returns true if the value is in the array, and false if the value is not in the array. From the php documentation, the php in_array() function takes 3 parameters: in_array(mixed $needle, array $haystack, bool $strict = […]
Creating Custom category.php With Pagination
Creating a custom category.php page with pagination in WordPress is very useful when we want to put additional content on our category pages. The code below is an example of how you can create a custom category.php page with pagination in WordPress. <?php get_header(); ?> <?php $article = get_term(get_queried_object()->term_id, 'category'); $category_name = $article->slug; $paged = […]
How to Check If String Contains Substring in PHP
PHP provides the strpos() function to check if a string contains a specific substring or not. The strpos() function returns the position of the first occurrence of a substring in a string. If substring not found, it will return false. For example, let’s say you were checking the string “I love to program” to see […]