Found 1060 Articles for PHP

Instance child class in abstract static method PHP?

AmitDiwan
Updated on 13-Oct-2020 08:40:06

546 Views

For this, use $anyObjectName=new static() along with self.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputJohn Doe

Generate array of random unique numbers in PHP?

AmitDiwan
Updated on 13-Oct-2020 08:37:21

937 Views

For random unique numbers array, use range() along with shuffle().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => 11 [1] => 14 [2] => 15 [3] => 12 [4] => 18 )

Combine three strings (day, month, year) and calculate next date PHP?

AmitDiwan
Updated on 13-Oct-2020 08:35:44

572 Views

You need to iterate using three for loops and lookup in given day, month and year. If the day, month and year is available, then put them into a variable.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe next date value is =2018-05-25

Remove new lines from a string and replace with one empty space PHP?

AmitDiwan
Updated on 13-Oct-2020 08:10:30

800 Views

Let’s say the following is our string with new line $sentence = " My name is John Smith My Favorite subject is PHP. ";We need to remove the new line above and replace with a whitespace i.e. the output should be −My name is John Smith My Favourite subject is PHP.For this, use trim() and withing that preg_replace() to replace.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputMy name is John Smith My Favorite subject is PHP.

How do I split the letters and numbers to two arrays from a string in PHP

AmitDiwan
Updated on 13-Oct-2020 08:07:07

554 Views

For this, use preg_split() along with array_shift() and array_pop(). Let’s say the following is our string with letters and numbers $values = "5j4o5h8n";We want to display the numbers and letters in separate arrays.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => j [1] => o [2] => h [3] => n ) Array ( [0] => 5 [1] => 4 [2] => 5 [3] => 8 )

Print the time an hour ago PHP?

AmitDiwan
Updated on 13-Oct-2020 08:04:40

5K+ Views

To print time an hour ago, you need to subtract -1 hour. Use the strtotime() method and set the subtraction value in it i.e. -1 for 1 hour ago time, -2 for 2 hour ago time, etc.Let’s say the current time is 18:42:22ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputOne hour ago, the date = 2020-09-26 17:42:22

How to call a function from a string stored in a variable PHP?

AmitDiwan
Updated on 13-Oct-2020 08:03:11

2K+ Views

To call a function from a string stored in a variable, use $func.The syntax is as follows $func=’anyFunctionName’;ExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputCalling hello method.

How to do bold and simple text in the same line using the echo in php?

AmitDiwan
Updated on 13-Oct-2020 08:01:37

3K+ Views

In order to get bold text, you need to use font-weight and to get the simple text you can use tag. All this gets added in the echo.ExampleThe PHP code is as follows Live Demo OutputHere is the snapshot of sample output.This is my first PHP Program

How to deal with multi-byte UTF-8 strings in JavaScript and fix the empty delimiter/separator issue

AmitDiwan
Updated on 13-Oct-2020 07:59:11

266 Views

For this, at first use preg_split() and set the function with this −'//u'After setting like above, the empty delimiter issue will get fixed, since we added '//u' above.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output −Array ( ) Array ( [0] => J [1] => o [2] => h [3] => n [4] => [5] => S [6] => m [7] => i [8] => t [9] => h )

Foreach loop with two arrays and if-condition evaluation to find matching values PHP?

AmitDiwan
Updated on 13-Oct-2020 07:50:00

3K+ Views

Let’s say we have the following two arrays $firstArray=array(10,20,30,40,50); $secondArray=array(100,80,30,40,90);We need to find the matching i.e. the output should be 30 40ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output The matching result is=30 The matching result is=40

Advertisements