PHP Articles

Page 27 of 81

Remove new lines from string in PHP

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 743 Views

To remove newlines from the string, the code is as follows−ExampleOutputThis will produce the following output−Demo     text for reference Demo text for referenceExampleLet us now see another example −OutputThis will produce the following output−Demo     text Demo text

Read More

Removing Array Element and Re-Indexing in PHP

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 307 Views

To remove an array element and re-index the array, the code is as follows−ExampleOutputThis will produce the following output−Array with leading and trailing whitespaces... Value = John Value = Jacob Value = Tom Value = Tim Comma separated list... John , Jacob , Tom , Tim Updated Array... Value = John Value = Jacob Value = Tom Value = Tim Updated Array...Re-indexed Value = John Value = Tom Value = Tim

Read More

How to remove the first character of string in PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

To remove the first character of a string in PHP, the code is as follows−ExampleOutputThis will produce the following output −Before removing the first character = Test After removing the first character = estExampleLet us now see another exampleOutputThis will produce the following output−Before removing the first character = Demo After removing the first character = emo

Read More

Reset keys of array elements using PHP ?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

To reset keys of array elements using PHP, the code is as follows−ExampleOutputThis will produce the following output−array(4) {    ["p"]=>    string(3) "150"    ["q"]=>    string(3) "100"    ["r"]=>    string(3) "120"    ["s"]=>    string(3) "110" } array(4) {    [0]=>    string(3) "150"    [1]=>    string(3) "100"    [2]=>    string(3) "120"    [3]=>    string(3) "110" }ExampleLet us now see another example −OutputThis will produce the following output−array(4) {    [8]=>    string(3) "Ben"    [4]=>    string(5) "Kevin"    [7]=>    string(4) "Mark"    [3]=>    string(5) "Hanks" } array(4) {    [0]=>    string(3) "Ben"    [1]=>    string(5) "Kevin"    [2]=>    string(4) "Mark"    [3]=>    string(5) "Hanks" }

Read More

Return all dates between two dates in an array in PHP

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 3K+ Views

To return all dates between two dates, the code is as follows −ExampleOutputThis will produce the following output−array(11) {    [0]=>    string(10) "10-11-2019"    [1]=>    string(10) "11-11-2019"    [2]=>    string(10) "12-11-2019"    [3]=>    string(10) "13-11-2019"    [4]=>    string(10) "14-11-2019"    [5]=>    string(10) "15-11-2019"    [6]=>    string(10) "16-11-2019"    [7]=>    string(10) "17-11-2019"    [8]=>    string(10) "18-11-2019"    [9]=>    string(10) "19-11-2019"    [10]=>    string(10) "20-11-2019" }

Read More

What is the difference between echo, print, and print_r in PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 6K+ Views

The print and echo are both language constructs to display strings. The echo has a void return type, whereas print has a return value of 1 so it can be used in expressions. The print_r is used to display human-readable information about a variable.ExampleLet us now see an example that displays output using echo, print, and print_r:OutputThis will produce the following output−Array... Value = John Value = Jacob Value = Tom Value = Tim Displaying Array Values using print... Value = John Value = Jacob Value = Tom Value = Tim Displaying Array Values using print_r... Array (    [0] => John    [1] => Jacob    [2] => Tom    [3] => Tim )

Read More

What is the difference between array_merge and array + array in PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 244 Views

Both get the union of arrays, but array_merge() overwrites duplicate non_numeric keys. Let us now see an example of the array+array−ExampleOutputThis will produce the following output−array(8) {    ["p"]=>    string(3) "150"    ["q"]=>    string(3) "100"    ["r"]=>    string(3) "120"    ["s"]=>    string(3) "110"    ["t"]=>    string(3) "115"    ["u"]=>    string(3) "103"    ["v"]=>    string(3) "105"    ["w"]=>    string(3) "125" }ExampleLet us now see an example of array_merge() in PHP−OutputThis will produce the following output−array(8) {    ["p"]=>    string(3) "150"    ["q"]=>    string(3) "100"    ["r"]=>    string(3) "120"    ["s"]=>    string(3) "110"    ["t"]=>    string(3) "115"    ["u"]=>    string(3) "110"    ["v"]=>    string(3) "105"    ["w"]=>    string(3) "100" }

Read More

Creating anonymous objects in PHP

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 649 Views

Beginning from PHP version 7, it has been made possible to create anonymous classes. Every object in PHP is associated with a class. The anonymous classes can be instantiated to create objects.ExampleOutputDoes the instance belong to parent class? = bool(true)In the above code, a parent class (my_sample_class) has been created, and it has been instantiated with a child class (new class) that inherits from the parent class.We are checking if the instance belongs to the parent class. Since the child class is an extension of the parent class, it returns True as output.

Read More

How to strip all spaces out of a string in PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 417 Views

To strip all spaces out of a string in PHP, the code is as follows−ExampleOutputThis will produce the following output−ThisisateststrinTo remove the whitespaces only, the below code can be used−ExampleOutputThis will produce the following output. The str_replace function replaces the given input string with a specific character or string−thisisateststringTo remove whitespace that includes tabs and line end, the code can be used−ExampleHere, the preg_match function is used with regular expressions. It searches for a pattern in a string and returns True if the pattern exists and false otherwise. This will produce the following output−thisisateststring

Read More

A Preferred method to store PHP arrays (json_encode or serialize)?\\n

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 501 Views

This depends on the requirements in hand.JSON is quicker in comparison to PHP serialization unless the following conditions are met−Deeply nested arrays are stored.The objects that are stored need to be unserialized to a proper class.The interaction is between old PHP versions that don't support json_decode.The below line of code can be used to store PHP arrays using json_encode−json_encode($array, JSON_UNESCAPED_UNICODE)JSON doesn't store the object's original class anywhere, but it can be restored as class instances belonging to stdClass.Why use json_encode instead of serializing?JSON is much more portable in comparison to serialize.The features of __sleep() and __wakeup() can't be leveraged using ...

Read More
Showing 261–270 of 801 articles
« Prev 1 25 26 27 28 29 81 Next »
Advertisements