PHP Articles

Page 13 of 81

If elseif else or ternary operator to compare numbers PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 460 Views

You can use any of them, but a more professional approach is using the ternary operator (?). Performance of both if-else and ternary operator is the same.Let’s say we have the following two variable values −$value1=10; $value2=0;We need to compare the above 2 variable values.Example This will produce the following outputOutputThe value is=550000

Read More

PHP How to add backslash inside array of strings?\\n

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 618 Views

To add backslash inside array of strings, use json_encode().Let’s say the following is our array −$value = [    "ADD", "REMOVE","SELECT","MODIFY" ];We want the output with backslash inside array of strings i.e −"["ADD","REMOVE","SELECT","MODIFY"]"ExampleThe PHP code is as follows − OutputThis will produce the following output"["ADD","REMOVE","SELECT","MODIFY"]"

Read More

How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 404 Views

The syntax is as follows −$anyVariableName=yourCondition ? if condition becomes true then this will be executed: if condition becomes false then this gets executed;ExampleThe PHP code is as follows OutputThis will produce the following outputGreater Than or Equal to 100

Read More

Generate random number from 0 – 9 in PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 561 Views

To generate random number in PHP, use rand(). Let’s say the following is our input −$storedValue ='0123456789';And we want to display a random value from the above values.Example OutputThis will produce the following outputThe random value=3

Read More

PHP fetch a specific value that begins with a given number from a string with letters and numbers?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 213 Views

Let’s say the following is our string with letters and numbers −$value ="1045632245555fghjklm67535454663443gfdjkbvc9890000006777743";We ant a specific value beginning from 989 i.e.−9890000006777743ExampleFor this, use substr() along with strpos(). OutputThe actual value is=1045632245555fghjklm67535454663443gfdjkbvc9890000006777743 The filter value is=9890000006777743

Read More

Manipulate for loop to run code with specific variables only PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 286 Views

Let’s say the following is our array −$marks=[45,67,89,34,98,57,77,30];And we want the output like this with only selected values45 67 89 68 98 57 77 60Above, we multiplied marks less than 40 with 2, rest kept the entire array same.ExampleThe PHP code is as follows OutputThis will produce the following outputThe Marks are as follows 45 67 89 68 98 57 77 60

Read More

Display game is in BUY or TRY mode PHP conditions?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 93 Views

For such conditional evaluation, use PHP if(), else if() and else.ExampleThe PHP code is as follows OutputGAME IS IN BUY MODE

Read More

PHP Inherit the properties of non-class object?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 202 Views

For this, use clone along with set the property name to null.ExampleThe PHP code is as follows OutputThis will produce the following outputobject(stdClass)#1 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } object(stdClass)#2 (2) { ["Name"]=> string(4) "John" ["Age"]=> int(20) } object(stdClass)#2 (2) { ["Name"]=> NULL ["Age"]=> NULL } object(stdClass)#1 (2) { ["Name"]=> NULL ["Age"]=> NULL }

Read More

Replacing specific characters from a matched string in PHP regular expression where we don't know the number of instances of the match?\\n

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 200 Views

For this, use preg_replace() in PHP. You need to also use Regular Expressions. Let’s say the following is our input −FirstName|John |LastName|Smith|SalaryProvided|2000|5000The expected output is as follows wherein we have replaced a specific character “|” with a whitespace. This character was placed between two numbers 2000 and 5000 −FirstName|John |LastName|Smith|SalaryProvided|2000 5000ExampleThe PHP code is as follows OutputThis will produce the following output The result is= FirstName|John |LastName|Smith|SalaryProvided|2000 5000

Read More

How to pass reference parameters PHP?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 582 Views

In PHP, use &$ in function parameter to pass reference parameters.ExampleThe PHP code is as follows OutputThis will produce the following output The actual value is=900 The modified value is=1000

Read More
Showing 121–130 of 801 articles
« Prev 1 11 12 13 14 15 81 Next »
Advertisements