Convert Amount to Float and Round Correctly in PHP

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

249 Views

To round amounts, use round() in PHP. Let’s say the following are our input values −$amount=50.78; $quantity=45.5;Convert them to float like this $am=(float) round($amount, 4); $quant=(float) round($quantity, 4);ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output50.78 45.5 The result is=2310.49

Pass Reference Parameters in PHP

AmitDiwan
Updated on 13-Oct-2020 07:27:47

549 Views

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

PHP preg_split to Split a String with Specific Values

AmitDiwan
Updated on 13-Oct-2020 07:19:39

337 Views

For this, use preg_match_all(). Let’s say the following is our string $values = 'javamysqlphpmongodbpythonspringhibernatephp';We want to split with specific values like java hibernate phpExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputArray ( [0] => Array ( [0] => java [1] => php [2] => hibernate [3] => php ) [1] => Array ( [0] => java [1] => php [2] => hibernate [3] => php ) )

Replacing Specific Characters in PHP Regular Expression

AmitDiwan
Updated on 13-Oct-2020 07:16:27

180 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 Live Demo OutputThis will produce the following output The result is= FirstName|John |LastName|Smith|SalaryProvided|2000 5000

Inherit Properties of Non-Class Object in PHP

AmitDiwan
Updated on 13-Oct-2020 07:14:18

173 Views

For this, use clone along with set the property name to null.ExampleThe PHP code is as follows Live Demo 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 }

Reading and Storing a List as an Array in PHP

AmitDiwan
Updated on 13-Oct-2020 07:09:49

195 Views

For this, you can simply use for loop.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output10 20 30 40 50 60

Display Game Buy or Try Mode in PHP Conditions

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

75 Views

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

Manipulate For Loop to Run Code with Specific Variables in PHP

AmitDiwan
Updated on 13-Oct-2020 07:05:02

246 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 Live Demo OutputThis will produce the following outputThe Marks are as follows 45 67 89 68 98 57 77 60

Fetch Specific Value Starting with Given Number from String in PHP

AmitDiwan
Updated on 13-Oct-2020 07:02:42

180 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(). Live Demo OutputThe actual value is=1045632245555fghjklm67535454663443gfdjkbvc9890000006777743 The filter value is=9890000006777743

Generate Random Number from 0 to 9 in PHP

AmitDiwan
Updated on 13-Oct-2020 06:59:58

527 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 Live Demo OutputThis will produce the following outputThe random value=3

Advertisements