Found 1060 Articles for PHP

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

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

383 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

507 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

185 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

196 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

How to remove null values with PHP?

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

733 Views

To remove null value in PHP, use array_filter(). It filters the array values. Let’s say the following is our array −$studentDetails = array("firstName" => "John",  "lastName"=> null); echo "The original value is=";print_r($studentDetails);Let’s filter with array_filter() −$result = array_filter($studentDetails);Example Live Demo OutputThe original value is=Array ( [firstName] => John [lastName] => ) After removing null part,the result is=Array ( [firstName] => John )

How to convert array to string PHP?

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

384 Views

To convert array to string, use the concept of implode () in PHP. Let’s say the following is our array −$sentence = array('My','Name','is','John');To convert the above array to string −,implode(" ",$sentence)Example Live Demo OutputThe string is = My Name is JohnLet us now see another PHP code wherein we will add separator as well −Example Live Demo OutputThe string is = One*Two*Three

PHP compression Stream Wrappers

Malhar Lathkar
Updated on 22-Sep-2020 10:51:29

477 Views

IntroductionIn PHP, zlib://, bzip2:// and zip:// represent wrappers for respective compression streams.compress:zlib://This works similar to gzopen() function, however, it can be used with filesystem functions like fread() and others.compress://bzip2This is similar to bzopen() function. Both stream wrappers operate even on systems not capable of supporting fopencookie.zip://The ZIP extension registers this wrapper. From PHP 7.2.0 onwards, archives encrypted with passwords are supported. It is possible to set password with password context option.Exampleszlib compression can be applied with following PHP codeTo uncompress, we can use following syntaxWe can also use built-in copy() function to build compressed zlib file and uncompress the samecopy('file.txt', ... Read More

Advertisements