Found 1046 Articles for PHP

Removing duplicate elements from an array in PHP

AmitDiwan
Updated on 01-Jul-2020 08:30:36

603 Views

The ‘array_flip’ function can be used, that will reverse the values as indices and keys as values.Example Live DemoOutputThe original array contains Array (    [0] => 45    [1] => 65    [2] => 67    [3] => 99    [4] => 81    [5] => 90    [6] => 99    [7] => 45    [8] => 68 ) The array after removing duplicate elements is Array (    [0] => 45    [1] => 65    [2] => 67    [3] => 99    [4] => 81    [5] => 90    [6] => 68 )An array ... Read More

PHP program to find missing elements from an array

AmitDiwan
Updated on 01-Jul-2020 08:32:02

890 Views

The ‘array_diff’ function can be used to find the elements that are missing from the array.Example Live DemoOutputElements missing from first array are Array (    [1] => 46    [2] => 47    [4] => 49    [5] => 50    [9] => 54    [10] => 55 ) Elements missing from second array are Array (    [1] => 100    [3] => 102    [4] => 103 )A function named ‘absent’ is defined that checks to see the minimum number and the maximum number and generates an array within that range. The function then returns the difference between this array and the original array, using the ‘array_diff’ function, that gives the missing elements from the array.

PHP program to find standard deviation of values within an array

AmitDiwan
Updated on 01-Jul-2020 08:33:25

703 Views

To find standard deviation of values within an array, the code is as follows in PHP −Example Live DemoOutputThe standard deviation of elements in the array is 35.423156268181A function named ‘std_deviation’ is defined counts the number of elements in the array and initializes the variance to 0. The average is calculated as the sum of elements in the array divided by the total number of elements in the array. Now, a ‘foreach’ loop is run over the array and the variance is calculated by subtracted the average value from every element of the array and squaring it.When the foreach loop goes ... Read More

Redirection in PHP

AmitDiwan
Updated on 01-Jul-2020 07:19:49

6K+ Views

The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client).Syntax of header functionheader( $header_value, $replace_value, $http_response_code)Following are the parameters −The ‘header_value’ in the function is used to store the header string.The ‘replace_value’ parameter stores the value that needs to be replaced.The ‘response_code’ is used to store the HTTP response code.ExampleThe website to which the page needs to be redirected is specified in the header function.

The internal working of the ‘foreach’ loop in PHP

AmitDiwan
Updated on 01-Jul-2020 07:18:25

242 Views

The ‘foreach’ loop in PHP helps in accessing key value pairs within an array. The ‘foreach’ loop works with arrays only, with the advantage that a loop counter wouldn’t need to be initialized. In addition to this, no condition needs to be set that would be needed to exit out of the loop. The ‘foreach’ loop implicitly does this too.Example Live DemoOutputJoe Hannah Paul SannaAn array of string values is defined and the foreach loop is used to access every element inside the array by giving a variable name. The ‘echo’ is used to display every element of the array on ... Read More

How to delete an element from an array in PHP and re-index the array?

AmitDiwan
Updated on 01-Jul-2020 08:35:38

2K+ Views

The ‘unset’ function can be used to remove an element from the array and use the ‘array_values’ function that resets the indices of the array.Example Live DemoOutputThe array is array(5) {    [0]=>    string(4) "this"    [1]=>    string(2) "is"    [2]=>    string(1) "a"    [3]=>    string(6) "sample"    [4]=>    string(4) "only" } The array is now array(4) {    [0]=>    string(4) "this"    [1]=>    string(2) "is"    [2]=>    string(1) "a"    [3]=>    string(6) "sample" }An array is declared that contains string values. The array is displayed and the ‘unset’ function is used to delete a specific index element from the array. Then the array is displayed again to reflect the changes on the console.

How to check if a string contains a specific sub string?

AmitDiwan
Updated on 01-Jul-2020 07:14:54

615 Views

To check if a string contains a specific substring, the ‘strlen’ and ‘strpos’ functions can be used. The ‘strlen’ gives the entire length of the string. The function ‘strpos’ finds the position wherein the substring occurs first in the string.Example Live DemoOutputThe substring is present within the string.Two strings are defined and the position of the second string in the first string is checked with the help of the ‘strpos’ function and compared with the length of the first string. Relevant message is displayed on the console.

What is the significance of ‘^’ in PHP?

AmitDiwan
Updated on 01-Jul-2020 08:37:23

214 Views

The ‘^’ is a bitwise operator in PHP, i.e XOR (exclusive OR) bitwise operator, that is used to display the ASCII values of variables in question. For example − For evert bit in the value, the ^ operator checks if that bit is the same in the other value too. If the values are same, 0 is produced as an output, otherwise 1 is shown as output. Below is an example illustrating the same −Example Live DemoOutputb aDifferent variables are assigned character values and the ‘^’ operator is used to perform XOR on the two variables. Relevant output is displayed on the console.

What are late static bindings in PHP?

AmitDiwan
Updated on 01-Jul-2020 07:11:33

987 Views

The basic idea behind late static bindings is that the concept of inheritance and the concept of ‘self’ keyword don’t follow the same rules. For example, a method ‘fun’ in parent class called in the child class won’t make ‘self’ refer to the child (as expected).The concept of late static bindings brings in a new keyword ‘static’, which when used, binds the function to the runtime class, or the class where the function was first used. In addition to this, any static function or variable is usually executed during runtime and not during compile time. Hence, if a value needs ... Read More

What is the meaning and usage of ‘=&’ assignment operator in PHP?

AmitDiwan
Updated on 01-Jul-2020 07:10:22

492 Views

Instead of copying the data from one variable to another one, the changes made to an array or object can be made to another using the ‘=&’ operator. This is known as the ‘assignment by reference’ method, which means both the values or objects will point to the same data, and no copies of the data are made. This way, data redundancy is avoided.Example Live DemoOutputThe value is : 89Inside the tags, two values are declared wherein the second value is the assignment by reference of the first value. Next, the value of the first variable is changed and the ... Read More

Advertisements