AmitDiwan has Published 10740 Articles

Reading and storing a list as an array PHP

AmitDiwan

AmitDiwan

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

213 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 is in BUY or TRY mode PHP conditions?

AmitDiwan

AmitDiwan

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

89 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 only PHP?

AmitDiwan

AmitDiwan

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

278 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 ... Read More

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

AmitDiwan

AmitDiwan

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

204 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 – 9 in PHP?

AmitDiwan

AmitDiwan

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

551 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

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

AmitDiwan

AmitDiwan

Updated on 13-Oct-2020 06:56:46

389 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 Live Demo OutputThis will produce the following outputGreater Than or Equal to 100

PHP How to add backslash inside array of strings?

AmitDiwan

AmitDiwan

Updated on 13-Oct-2020 06:52:24

608 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 − Live Demo OutputThis ... Read More

Remove extra whitespace from the beginning PHP?

AmitDiwan

AmitDiwan

Updated on 13-Oct-2020 06:48:58

205 Views

If you want to remove extra whitespace from the beginning, use ltrim() in PHP. Let’s say the following is our variable −$value="  Hello, welcome to PHP Tutorial  ";We want the output with no whitespace on the left −Hello, welcome to PHP TutorialExample Live Demo OutputThis will produce ... Read More

Is there a way to change the date format in php?

AmitDiwan

AmitDiwan

Updated on 13-Oct-2020 06:45:20

166 Views

Yes, change the date format in PHP using the date() along with strtotime(). Let’s say the following is our date −$dateValue = "2020-09-19";Change the date format using the strtotime() method −$modifiedDate= date("d-m-Y", strtotime($dateValue));  Example Live Demo OutputThis will produce the following outputThe original date format is=2020-09-19 The ... Read More

If elseif else or ternary operator to compare numbers PHP?

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 14:02:03

443 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 Live Demo ... Read More

Advertisements