Server Side Programming Articles - Page 1524 of 2650

Convert timestamp coming from SQL database to String PHP?

AmitDiwan
Updated on 12-Oct-2020 13:26:15

479 Views

To convert timestamp to string, use setTimestamp(). Let’s say the following is our input i.e. timestamp −$SQLTimestamp = 1600320600000;We want the following output i.e. Date string −2020-09-17 05:30:00At first, get seconds from Timestamp −$seconds = round($SQLTimestamp/1000, 0);Example Live Demo Output2020-09-17 05:30:00

How to build dynamic associative array from simple array in php?

AmitDiwan
Updated on 12-Oct-2020 13:20:17

2K+ Views

Let’s say we have the following array −$namesArray = ['John', 'Adam', 'Robert'];We want the following output i.e. an associative array from the above array −Array ( [John] => Array ( [Adam] => Array ( [Robert] => Smith ) ) )Example Live Demo OutputArray ( [John] => Array ( [Adam] => Array ( [Robert] => Smith ) ) ) Doe

How to insert into a table all combination of values in PHP arrays?

AmitDiwan
Updated on 12-Oct-2020 13:14:21

1K+ Views

For this, use the foreach loop and insert into statement in order to insert all combinations of values in PHP arrays.Let’s say we have the following arrays −$name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"];Example Live Demo

Display a two-dimensional array with two different nested loops in matrix form PHP?

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

397 Views

Use two loops, one for row and another for column. Fr matrix form, use the tab \t in the nested loop like his −twoDimensionalArray[row][col],"\t";Example Live Demo Output777 777 777Above, you can see the output is in matrix form 3x3.

How to add http:// if it doesn't exist in the URL PHP?

AmitDiwan
Updated on 12-Oct-2020 13:01:17

518 Views

Here, we have set a function that adds "http:// to a string. Let’s say we have passed the following value −example.comAnd the output we want is with "http://" i.e. an actual link −http://example.com For this, you can use dot(.) notation and conditional match with preg_match().Example Live Demo Outputhttp://example.com https://example.com

Please explain what happens when PHP switch case executes case 0?

AmitDiwan
Updated on 12-Oct-2020 12:57:59

197 Views

The PHP is a loosely typed language. When you match with case 0 the string matches with closest integer.Let’s say we have the following switch expression −switch ("match")Now, we will match with case 0 −case 0:     echo " 0 with match";     break;We will also match for non-zero case −case "match":     echo "match successful";     break;Example Live Demo Output0 with match

Shorten string with a “…” body in PHP?

AmitDiwan
Updated on 12-Oct-2020 12:54:26

215 Views

Let’s say the following is our string −$sentence="This is my first PHP program";We want the following output −This is my first PHP ... gramWe shorten the string with “…”. For this, use the concept of substring() and set in a condition by checking the number of words −Example Live Demo OutputThis is my first PHP ... gram

How to check a file is video type or not in php?

AmitDiwan
Updated on 12-Oct-2020 12:51:18

2K+ Views

Let’s say the following is our variable wherein we have an .mp4 path file −$movieFileType="demo.mp4";To check whether the above file is video type, use end() along with explode(). You need to set this in strtolower() and check the condition −if(strtolower(end(explode(".",$movieFileType))) =="mp4") { } else { }Example This will produce the following output −OutputThe movie demo.mp4 is of video type.

How to access and return specific value of a foreach in PHP?

AmitDiwan
Updated on 12-Oct-2020 12:42:22

1K+ Views

You can use the below syntax to access the value of a foreach.The syntax is as follows −foreach ($yourArrayName as &$anyVariableName)Let’s say we have the following array:$values= array(35, 50, 100, 75);We will now multiple each array value with 5 using the following PHP code −Example Live Demo Output175 250 500 375

Get Root Directory Path of a PHP project?

AmitDiwan
Updated on 12-Oct-2020 12:22:46

23K+ Views

In order to get the root directory path, you can use _DIR_ or dirname().The syntax is as follows −echo _DIR_;The second syntax is as follows−echo dirname(__FILE__);Both the above syntaxes will return the same result.Example Live Demo Output/home/KOq8Zd /home/KOq8Zd

Advertisements