Found 1046 Articles for PHP

Reference assignment operator in PHP to assign a reference?

AmitDiwan
Updated on 12-Oct-2020 13:41:24

211 Views

Let’s say we have the following value −$nextValue=100;Let us take a new variable and assign a reference −$currentValue = &$nextValue;To assign a reference, use the reference assignment operator i.e. =&$anyVariableName in PHP.The PHP code is as follows −Example Live Demo OutputNext Value=100 Current Value=100 After changing the value of next will reflect the current value because of => Next Value=45000 Current Value=45000

PHP print keys from an object?

AmitDiwan
Updated on 12-Oct-2020 13:35:45

5K+ Views

Let’s say the following is our object −$employeeDetails = (object) [     'firstName' => 'John',     'lastName' => 'Doe',     'countryName' => 'US' ];We want the following output i.e. only the keys −firstName lastName countryNameTo display only the keys from an object, use array_keys() in PHP.Example Live Demo OutputAll Keys are as follows= firstName lastName countryName

Convert timestamp coming from SQL database to String PHP?

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

377 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

1K+ 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

842 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

311 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

376 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

124 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

136 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

1K+ 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.

Advertisements