Found 33676 Articles for Programming

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

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 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

205 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

736 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

388 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

Program to check minimum number of characters needed to make string palindrome in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:04:58

489 Views

Suppose we have a string s, we have to find the minimum number of characters needed to be inserted so that the string becomes a palindrome.So, if the input is like s = "mad", then the output will be 2, as we can insert "am" to get "madam".To solve this, we will follow these steps −Define a function dp(). This will take i, jif i >= j, thenreturn 0if s[i] is same as s[j], thenreturn dp(i + 1, j - 1)otherwise, return minimum of dp(i + 1, j) and dp(i, j - 1) + 1From the main method, do the ... Read More

Program to find an ancestor which is common of two elements in a binary tree in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:01:59

248 Views

Suppose we have a binary tree, and we also have two numbers a and b, we have to find the value of the lowest node that has a and b as descendants. We have to keep in mind that a node can be a descendant of itself.So, if the input is likea = 6, b = 2, then the output will be 4To solve this, we will follow these steps −Define a method solve() this will take root and a, bif root is null, thenreturn -1if value of root is either a or b, thenreturn value of rootleft := solve(left ... Read More

Program to find minimum length of lossy Run-Length Encoding in Python

Arnab Chakraborty
Updated on 10-Oct-2020 15:03:03

293 Views

Suppose we have a lowercase string s and another value k. Now consider an operation where we perform a run-length encoding on a string by putting repeated successive characters as a count and character. So if the string is like "aaabbc" would be encoded as "3a2bc". Here we do not put "1c" for "c" since it only appears once successively. So we can first remove any k consecutive characters in s, then find the minimum length possible of the resulting run-length encoding.So, if the input is like s = "xxxxxyyxxxxxzzxxx", k = 2, then the output will be 6, as ... Read More

Advertisements