Server Side Programming Articles - Page 1737 of 2650

PHP program to find standard deviation of values within an array

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

939 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

7K+ 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

277 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

767 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

432 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

1K+ 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

868 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

How does the array_diff_key() work in PHP?

AmitDiwan
Updated on 01-Jul-2020 08:39:46

173 Views

It is an inbuilt function that compares the keys of one or more arrays, and returns their difference.Syntax of the array_diff_key functionarray array_diff_key($array1, $array2, ..)The function can take two or more array names as parameters, and compares the first array with the remaining arrays.Example Live DemoOutputArray (    [91] => Micheal )Inside the tags, three arrays are declared with certain values in them. They are printed by calling the ‘array_diff_assoc’ function by passing all the three arrays to it as parameters. The resultant value is the different between the first array, and second array as well as the difference ... Read More

PHP Predefined Mathematical Constants

Malhar Lathkar
Updated on 30-Jun-2020 07:36:18

203 Views

Definition and UsageConstantValueDescriptionM_PI3.14159265358979323846PiM_E2.7182818284590452354Euler Number eM_LOG2E1.4426950408889634074log2 eM_LOG10E0.43429448190325182765log10 eM_LN20.69314718055994530942loge 2M_LN102.30258509299404568402loge10M_PI_21.57079632679489661923pi/2M_PI_40.78539816339744830962pi/4M_1_PI0.318309886183790671541/piM_2_PI0.636619772367581343082/piM_SQRTPI1.77245385090551602729sqrt(pi)M_2_SQRTPI1.128379167095512573902/sqrt(pi)M_SQRT21.41421356237309504880sqrt(2)M_SQRT31.73205080756887729352sqrt(3)M_SQRT1_20.707106781186547524401/sqrt(2)M_LNPI1.14472988584940017414loge(pi)M_EULER0.57721566490153286061Euler constantPHP_ROUND_HALF_UP1Round halves upPHP_ROUND_HALF_DOWN2Round halves downPHP_ROUND_HALF_EVEN3Round halves to even numbersPHP_ROUND_HALF_ODD4Round halves to odd numbersNANNANNot A NumberINFINFInfinity

Advertisements