You are here: Home / Topics / Search / PHP
Showing 66 Search Results for : PHP

How to sort an associative array by value in PHP

Filed under: PHP on 2023-07-02 21:56:04
The PHP functions asort() and arsort() can be used to sort an array by value. Sort an associative array by value in ascending order You can use the asort() function to sort an associative array alphabetically by value in ascending order, while maintaining the relationship between key and v

How to remove a specific element from an array in PHP

Filed under: PHP on 2023-07-02 21:26:17
If you want to remove an element from an array, you can just use the PHP unset() method. The following example shows you how to remove an element from an array. Example 1: How to remove a specific element from an associative array by key in PHP<?php $languages = array("p"=>"PHP", "j"

How to get a value from an array in PHP

Filed under: PHP on 2023-07-02 21:24:17
If you want to access a value in an indexed, associative, or multidimensional array, you can do it by using the index or the key. Example 1: How to get a value from an array <?php $numbers = array(1, 2, 3, 4, 5); echo $numbers[0];?>Output:1Example 2: How to get a value from

How to get all the keys of an associative array in PHP

Filed under: PHP on 2023-07-02 21:21:53
You can use the PHP array_keys() function to extract all the keys from an associative array. How to get all the keys of an associative array in PHP <?php $languages = array("p"=>"PHP", "j"=>"Java", "a"=>"Ada", "h"=>"HTML"); print_r(array_keys($languages));?>&nbs

How to Populate Dropdown List using Array in PHP

Filed under: PHP on 2023-07-02 21:20:14
In this tutorial, we are going to see how to populate dropdown list using array in PHP. You can simply use the foreach loop to create a <select> list or any drop-down menu that forms the values of an array. How to Populate Dropdown List using Array in PHP <!DOCTYPE html><htm

How to remove null or empty values from an array in PHP

Filed under: PHP on 2023-07-02 21:17:47
You can use the PHP array_filter() function to remove or filter empty values from an array. This function usually filters the values of an array using a callback function. However, if no callback is specified, all values in the array equal to FALSE will be removed, such as an empty string or a NULL 

How to calculate sum of an array in PHP

Filed under: PHP on 2023-07-02 21:16:20
You can use the PHP array_sum() function to calculate the sum of all numeric values in an array. As shown in the following example: How to calculate sum of an array in PHP <?php $arr1 = array(1, 2, 3); $arr2 = array("a" => 1, "b" => 2, "c" => 3); echo array_sum($

How to get random value from an array in PHP

Filed under: PHP on 2023-07-02 21:14:59
In this tutorial, we are going to see how to get random value from an array in PHP. You can use the PHP shuffle() function to randomly shuffle the order of elements or values in an array. The shuffle() function returns FALSE on failure. How to get random value from an array in PHP <?php

How to remove an array element based on key in PHP

Filed under: PHP on 2023-07-02 21:13:13
The unset() function is used to remove an element from an array. The unset() function is used to destroy any other variable and similarly to remove an element from an array. This function takes the array key as input and removes that element from the array. After removal, the associated key and valu

How to download a file in PHP using Curl

Filed under: PHP on 2023-07-02 21:09:16
Here you will find how to download a file using PHP by Using PHP Curl: <?php  $url = 'https://www.mcqbuddy.com/img/user.jpg';     // Initialize the cURL session $session = curl_init($url);     // Initialize the directory name where the f

How to Download file from URL using PHP

Filed under: PHP on 2023-07-02 21:07:35
There are several approaches to download a file from a URL. Some of them are described below: Using file_get_contents() function:file_get_contents() function is used to read a file into a string. This feature uses memory mapping techniques supported by the server and thus improves performance, 

How to open a PDF file in browser with PHP

Filed under: PHP on 2023-07-02 21:05:19
PHP uses standard code to display the pdf file in the web browser. The process of viewing the PDF file involves locating the PDF file on the server. It uses various types of headers to define the composition of the content in the form of Type, Layout, Transfer-Encode, etc. PHP transmits the PDF file

How to compare two dates in PHP

Filed under: PHP on 2023-06-30 06:38:36
Given two dates (date1 and date2) and the task is to compare the given dates. Comparing two dates in PHP is simple when the two dates are in the same format, but the problem arises when the two dates are in a different format. Method 1: How to compare two dates in PHPIf the given dates have the

How to Change Max Size of File Upload in PHP

Filed under: PHP on 2023-06-30 06:33:43
The maximum size of any file that can be uploaded to a website written in PHP is determined by the max_size values, mentioned in the server’s php.ini file. In case of a hosted server, you should contact the hosting server administrator. It is useful to create a local HTTP server for developers.Pro

How to get first day of week in PHP

Filed under: PHP on 2023-06-30 06:32:31
You can use strtotime() function to get the first day of the week using PHP. This function returns the timestamp of the default time variable, then uses date() function to convert the date from the timestamp to a meaningful date.In PHP the week starts with Monday, so if the time string is given as â

How to refresh a page with PHP

Filed under: PHP on 2023-06-30 06:30:12
Here you will find how to refresh a page with PHPYou can use header() function to refresh a web page in PHP. HTTP functions are the functions that manipulate the information sent to the client or browser by the web server before sending any other output.header() function in PHP sends an HTTP header 

How to declare a global variable in PHP

Filed under: PHP on 2023-06-30 06:28:16
Global variables refer to any variable defined outside the function. Global variables are accessible from any part of the script, i.e. inside and outside the function.There are two ways to access a global variable inside a function:Using the global keywordUsing the array GLOBALS[var_name]: It stores

How to Get IP Address of Server in PHP

Filed under: PHP on 2023-06-30 06:26:52
Use the following code if you want to display the IP address of the server. Simply copy the code to a text file and save it as a .php file on the server.For Linux with Apache, use the following code: <?php echo $_SERVER['SERVER_ADDR'];?>For Windows 2008 R2 with IIS 7.5 Server, use:&l

How to get the client IP address in PHP

Filed under: PHP on 2023-06-30 06:24:56
In this tutorial, we are going to see how to get the client IP address in PHP. We often need to collect the client’s IP address to track activity and for security reasons. It is very easy to get the IP address of the visitor in PHP. The PHP variable $_SERVER makes it easy to get the IP address of 

How to check if file exists on remote server PHP

Filed under: PHP on 2023-06-30 06:22:44
In this tutorial, we are going to see how to check if file exists on remote server in PHP. The function file_exists() in PHP allows you to check if a file or a directory exists on the server. But the function file_exists() will not be usable if you want to check the existence of the file on a remote

How to Extract Content Between HTML Tags in PHP

Filed under: PHP on 2023-06-30 06:19:58
In this tutorial, we are going to see how to extract content between HTML tags in PHP. preg_match() function is the easiest way to extract text between HTML tags with REGEX in PHP. If you want to get content between tags, use regular expressions with the preg_match() function in PHP. You can also ex

How to Calculate Age from Date of Birth in PHP

Filed under: PHP on 2023-06-30 06:16:16
In this tutorial, we are going to see how to calculate age from Date of Birth in PHP. Some web applications need to display the user’s age. In this case, you need to calculate the user’s age from the date of birth. We are going to see a PHP code snippet to calculate the age from the date of birt

How to validate Email in PHP?

Filed under: PHP on 2023-04-16 07:46:15
In this tutorial, we are going to see how to validate an email address in PHP. Validating an email address is an action required before submitting the form. It checks if the user has provided a valid email address. Here is the easiest and most effective way to validate an email address in PHP.filter

What are cookies? How to create cookies in PHP?

Filed under: PHP on 2023-04-04 06:38:36
A cookie is a small record that the server installs on the client’s computer. They store data about a user on the browser. It is used to identify a user and is embedded on the user’s computer when they request a particular page. Each time a similar PC asks for a page with a program, it will send

Best PHP Frameworks For Web Development

Filed under: PHP on 2023-03-14 06:40:55
Following are some Frameworks which uses PHP:1. Laravel2. Phalcon3. Fat-Free Framework4. CodeIgniter5. Laminas Project6. CakePHP7. FuelPHP8. Slim9. PHPixie10. Symfony11. Yii

Introduction to PHP Language

Filed under: PHP on 2023-03-14 06:40:00
PHP language stands for “PHP: Hypertext Pre-processor”The most widely-used, open-source, and script language. All PHP scripts are executed on the server. It can be run on different platforms like:WindowsLinuxUnixMac OS X etc.It is used for almost every server likeApache It also support

How to sort elements of an array in descending order in PHP?

Filed under: PHP on 2022-11-19 12:32:53
The below program is to sort elements of an array in descending order using PHP sort() function. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time.sort() function in PHPExample<!DOCTYPE html><html>&l

Program to find the sum of elements in an Array in PHP

Filed under: PHP on 2022-11-19 12:30:07
The below program is to find the sum of elements in an array. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time.array_sum() function in PHP Example<!DOCTYPE html><html><body><?php  

How to find the product of elements in PHP array

Filed under: PHP on 2022-11-18 20:51:52
The below program is to find the product of elements in an array. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time.array_product() function in PHPExample<!DOCTYPE html><html><body><?php &nb

How to convert a string into Array elements in PHP

Filed under: PHP on 2022-11-17 06:48:22
The below program is to split a string as array elements based on delimiter using PHP explode() function. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time. The PHP explode() function is used to break a string i

How to join the array elements into a String

Filed under: PHP on 2022-11-17 06:45:06
The below program is to combine the array elements into a string with given delimiter using PHP join() function. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time. The PHP join() function is used to get a string

How to merge two arrays in a new array

Filed under: PHP on 2022-11-17 06:42:20
The below program is to merge two arrays into a new array using PHP array_merge() function. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time. The PHP array_merge() function is used to merge one or more arrays i

Remove The Duplicate Values From An Array in PHP

Filed under: PHP on 2022-11-16 13:51:50
The below program is to remove the duplicate values from an array using PHP array_unique() function. The PHP echo statement is used to output the result on the screen. An array is a single variable which can store multiple values at a time. The PHP array_unique() function is used to remove the dupli

PHP Program for checking Palindrome number

Filed under: PHP on 2022-11-16 13:41:54
The below program checks for a Palindrome number using a loop. The PHP echo statement is used to output the result on the screen. A number is called as a Palindrome number if the number remains same even when its digits are reversed.Palindrome Number:abcde = edcbaExample: 24142, 1234321, etc.Example

PHP Program to check Armstrong number

Filed under: PHP on 2022-11-16 13:39:41
The below program checks for an Armstrong number using a loop. The PHP echo statement is used to output the result on the screen. A number is called as an Armstrong number if the sum of the cubes of its digits is equal to the number itself.Armstrong Number:abc = (a*a*a) + (b*b*b) + (c*c*c)Example: 0

PHP Program to print table of any number?

Filed under: PHP on 2022-11-16 13:36:32
The below program prints table of a number using a loop. The PHP echo statement is used to output the result on the screen.Example<!DOCTYPE html><html><body><?php    $num = 9;  for($i=1; $i<=10; $i++)   {$product = $i*$num;echo "$num * $i = $product" 

How to check prime number in PHP?

Filed under: PHP on 2022-11-16 13:34:57
The below program checks if a number is a prime or a composite number. The PHP echo statement is used to output the result on the screen.Example<!DOCTYPE html><html><body><?php$num = 13;  $count=0;  for ( $i=1; $i<=$num; $i++)  {  if (($num%$i)==0)  {

PHP Program To Print Sum Of Digits

Filed under: PHP on 2022-11-16 13:33:14
The below program gives the sum of two variables x and y. The PHP echo statement is used to output the text and sum value to the screen. In a similar way, codes can be written to get a sum of multiple variables in PHP.Example<!DOCTYPE html>
<html>
<body>
 
<?php
$txt1 

PHP Program for finding the frequency of numbers in given array

Filed under: PHP on 2022-11-15 10:58:49
This program is useful for finding how many times any number is placed in any array. Suppose we have an array with elements as follows: $a = array (54,85,92,54,16,92,48);Output will be: 54 occurs 2 times 85 occurs 1 times 92 occurs 2 times 16 occurs 1 times 48 occurs 1 

PHP Program to Separate Zeros from the given Array elements

Filed under: PHP on 2022-11-15 10:51:43
Hello guys, this program is used to find all the zeros in any given array and shift those zeros in the last for example. Given array: 1 8 5 0 4 0 5 1 0 0 4 Output: 1 8 5 4 5 1 4 0 0 0 0Program:://To separate zeros from the given array elements<html>     <head>&nb

How to convert decimal numbers to binary in PHP

Filed under: PHP on 2022-11-15 10:45:25
This program is written is PHP and HTML to show how to convert decimal number to binary number. Note- there is a inbuilt function in PHP which is used for converting decimal numbers to binary. decbin() function is used for this purpose. Program::// To convert Decimal number into Binary num

Rules for naming variable in PHP

Filed under: PHP on 2022-10-31 21:30:28
A variable starts with the $ sign, followed by the name of the variableA variable name must start with a letter or the underscore characterA variable name cannot start with a numberA variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )Variable names are case-sen

Explain NULL in PHP

Filed under: PHP on 2022-10-31 21:28:10
In PHP, the special data type NULL is used to indicate the presence of solely the NULL value. It cannot have any other value assigned to it.PHP allows for the declaration of NULL in one of two ways, as illustrated below:$var = NULL:Or$var = null;In PHP, the two aforementioned syntaxes function corre

What definition does PHP give to constants?

Filed under: PHP on 2022-10-31 21:27:42
Utilizing PHP's define() function makes defining constants simple. The values of the constants can be simply defined and obtained using this function.Constants cannot be modified after definition, as the name implies. It is not necessary for them to begin with the standard $ sign as is done in PHP s

What kinds of constants are predefined in PHP?

Filed under: PHP on 2022-10-31 21:27:13
The following are some of the most popular constants in PHP:_METHOD_: Displays the class nameCLASS_: Provides the class name.FUNCTION_: Indicates the function name.LINE_: Indicates the working line number.FILE_: Displays the file name and its path.

Types of variable in PHP

Filed under: PHP on 2022-10-31 21:26:29
The following list of PHP's eight main data types:A named, sorted collection of data is called an array.A logical value is a boolean (True or False)Double: Numbers with floating points, like 5.1525Whole numbers without a floating point represent integers.An object is a class instance that includes d

What does PEAR in PHP stand for?

Filed under: PHP on 2022-10-31 21:25:08
PHP Extension and Application Repository is referred to as PEAR. All of the reusable PHP components are housed in one of the frameworks and acting repositories. It not only contains some of the PHP libraries but also gives you a straightforward interface to set up packages automatically.Learn PHP MC

What is the primary use of PHP?

Filed under: PHP on 2022-10-31 21:23:56
There are many applications for PHP for developers. The following are some of the most popular ideas that PHP provides:It is really simple to grant a website's necessary content-restricted access using PHP.Users can access certain cookies and set them in accordance with their needs.It is simple to d

Explain rand() function in PHP

Filed under: PHP on 2022-10-31 21:20:55
Hello guys, I am Pritvik Sharma. In this post, I am going to tell you what rand() function does in case of PHP. rand() function is inbuilt function in PHP which is used to generate a random integer. If you write rand() without its minimum and maximum value it will generate a random number from 

How to Check Website Availability with PHP

Filed under: PHP on 2022-10-30 21:30:30
Hello programmers, In this post, we are going to see how we can check website is active or not. We will make it possible with PHP and Curl. The following PHP script helps you to check if your website is online and available. If you want to check the status of your website, make a cURL request t

How to Generate Random String in PHP

Filed under: PHP on 2022-10-30 20:51:33
In this tutorial, we are going to see how to generate a random string in PHP. We can achieve that by storing all possible letters in a string and then generate a random index from 0 to the length of the string -1. As shown in the following code.Generate random string in php<?php 

function getR

Program for Factorial in PHP with PHP "for loop"

Filed under: PHP on 2022-10-30 19:47:51
In this post, we are going to see how to write a PHP function to calculate the factorial of a given number in PHP using for loop. The factorial of a number is the product of all integers between 1 and itself.Code<?php
  function fact($n){ 
    $f = 1; 
    for ($i = 1;

How to check if a URL is valid in PHP

Filed under: PHP on 2022-10-30 19:44:19
In this tutorial, we are going to see how to check if a URL is valid in PHP. When a URL is submitted, it is very important to check if this URL is valid or not before performing any action. Here we are going to see how to check if a URL is valid in PHP using filter_var() function with FILTER_VALIDAT

Difference between include () and require() function

Filed under: PHP on 2022-10-09 08:25:52
include() functionThis function is used to copy all the contents of a file called within the function, text wise into a file from which it is called.When the file is included cannot be found, it will only produce a warning (E_WARNING) and the script will continue the execution.require() function:The

For each loop in PHP

Filed under: PHP on 2022-10-09 07:42:53
The foreach statement is a looping construct that is used in PHP to iterate and loop through the array data type.The working of foreach is simple, with every single pass of the value, elements get assigned a value, and pointers are incremented. This process is repeatedly done until the end of the ar

Does JavaScript interact with PHP?

Filed under: PHP on 2022-10-08 13:50:29
JavaScript is a client-side programming language, whereas PHP is a server-side scripting language. PHP has the ability to generate JavaScript variables, and this can be executed easily in the browser. Thereby making it possible to pass variables to PHP using a simple URL.

What are traits in PHP?

Filed under: PHP on 2022-10-08 13:50:05
Traits are a mechanism that lets you create reusable code in PHP and similar languages where multiple inheritances are not supported. It’s not possible to instantiate it on its own.A trait is intended to reduce the limitations of single inheritance by enabling a developer to reuse sets of methods 

Types of errors in PHP

Filed under: PHP on 2022-10-08 13:48:26
The 3 main types of errors in PHP are:Notices: Notices are non-critical errors that can occur during the execution of the script. These are not visible to users. Example: Accessing an undefined variable.Warnings: These are more critical than notices. Warnings don’t interrupt the script execution. 

What is Parser in PHP?

Filed under: PHP on 2022-10-08 13:47:46
A PHP parser is software that converts source code into the code that computer can understand. This means whatever set of instructions we give in the form of PHP code is converted into a machine-readable format by the parser.You can parse PHP code with PHP using the token_get_all() function.

PHP and HTML interact

Filed under: PHP on 2022-10-08 13:42:18
PHP scripts have the ability to generate HTML, and it is possible to pass information from HTML to PHP.PHP is a server-side language whereas HTML is a client-side language. So PHP executes on the server-side and gets its results as strings, objects, arrays, and then we use them to display its values

Disadvantages of PHP Language

Filed under: PHP on 2022-10-08 13:41:19
The cons of PHP are:PHP is not suitable for giant content-based web applications.Since it is open-source, it is not secure. Because ASCII text files are easily available.Change or modification in the core behavior of online applications is not allowed by PHP.If we use more features of the PHP framew

Rules for naming a PHP variable

Filed under: PHP on 2022-10-08 08:40:21
The following two rules are needed to be followed while naming a PHP variable:A variable must start with a dollar symbol, followed by the variable name. For example: $price=100; where price is a variable name.Variable names must begin with a letter or underscore.A variable name can consist of letter

What are the different types of variables present in PHP?

Filed under: PHP on 2022-10-08 08:36:49
There are 8 primary data types in PHP which are used to construct the variables. They are:Integers: Integers are whole numbers without a floating-point. Ex: 1253.Doubles: Doubles are floating-point numbers. Ex: 7.876Booleans: It represents two logical states- true or false.NULL: NULL is a special ty

Is PHP a case-sensitive language?

Filed under: PHP on 2022-10-08 08:35:53
PHP can be considered as a partial case-sensitive language. The variable names are completely case-sensitive but function names are not. Also, user-defined functions are not case-sensitive but the rest of the language is case-sensitive.For example, user-defined functions in PHP can be defined in low

What is a session in PHP?

Filed under: PHP on 2022-10-08 08:34:46
A session in PHP is a way to store information to be used across multiple pages of an entire website. The information is not stored on the user’s computer, unlike cookies. In a temporary directory on the server, a file will be created by the session where registered session variables and their val