AmitDiwan has Published 10744 Articles

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

AmitDiwan

AmitDiwan

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

386 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

AmitDiwan

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

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

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

AmitDiwan

AmitDiwan

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

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

Shorten string with a “…” body in PHP?

AmitDiwan

AmitDiwan

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

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

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

AmitDiwan

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

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

AmitDiwan

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

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

AmitDiwan

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

735 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] => ... Read More

How to convert array to string PHP?

AmitDiwan

AmitDiwan

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

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

Pushing positives and negatives to separate arrays in JavaScript

AmitDiwan

AmitDiwan

Updated on 12-Oct-2020 11:51:51

392 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array.We will be using the Array.prototype.reduce() method to pick desired elements and put them into ... Read More

Advertisements