Compact takes individual variables, and packs them into an associative array. Extract does the opposite. <?php // example code // Set 2 variables $var1 = “one”; $var2 = “two”; // Create associative array $arr = compact(‘var1′,’var2’); // Show results var_dump($arr);
Splat!
While looking through the Query Builder class in the Laravel source code today, I came across something I hadn’t seen before in PHP: $query->{$method}(…array_values($value)); I had no idea what the three dots meant, so I went looking for it. Turns
Understanding Octal
Why would we need to understand Octal? The most relevant reason I can come up with right now is that looking back at some of the interview question challenges I’ve posted, there is one that deals with an Octal number.
foreach vs array_filter
The problem: Given an array of numbers, create a new array containing only the odd numbers from the original. $original_array = [1,2,3,4,5,6,7,8,9,10]; There are two ways you can approach this problem. Let’s look at a foreach loop first: $original_array =
Factor Finder
// Number to factor $num = 60; // Loop from the number to factor down to 1 for($i=$num; $i>=1; $i–){ // If it divides evenly if($num % $i === 0 ){ // Output result of division, which is a factor
Changing the color of the first word
The question/problem is: Write a PHP script, which changes the color of the first character of a word. This challenge tests your knowledge of preg_replace, and regular expressions. $text = ‘PHP Tutorial’; $text = preg_replace(‘/(\b[a-z])/i’,'<span style=”color: red;”>\1′,$text); echo $text; ?>
SQL sub query challenge
Goal: Write a query which will get the first and last name from the customer table, and the total amount of all that customer’s orders. SELECT first,last FROM customers C This will get all the customers.Next we need to write
Reference challenge
This one tests your knowledge of how reference variables work: $a = ‘P’; $b = &$a; $b = “PH$b”; What will $a and $b be after these three lines of code run? First, $a is set to ‘P’. Then, $b
Base class or Interface?
When making the decision to use a base class or interface, it may help to think of the implementation. If all the implementations are going to be similar, then you might want to group them under a base class. But
preg_match problem
Given: $a = “4|6|3|5|x|2|”; Write code that will output: “2|3|4|5|6” It could be done the long way round, by turning the string into an array, looping over each element, testing each item to see if it’s a number, and placing
MySql Limit
SELECT c.first_name,c.last_name, o.order_total FROM customers c LEFT OUTER JOIN orders o ON c.id = o.cust_id WHERE o.order_total > 100.0 I was once asked to write a query similar to this. The goal was to select all customers who had placed
Array Sorting
I came across this problem recently: How would you sort an array of strings to their natural case-insensitive order, while maintaining their original index association? array( ‘0’ => ‘z1’, ‘1’ => ‘Z10’, ‘2’ => ‘z12’, ‘3’ => ‘Z2’, ‘4’ =>
How to enable PHP error reporting
ini_set(‘display_errors, 1); error_reporting(E_ALL); The ini_set function sets a configuration option for the duration of the script execution. When the script is done, the value returns to whatever it was set to originally in the ini file. The error_reporting function
Coerced in two different ways
$x = NULL; if (‘0xFF’ == 255) { $x = (int)’0xFF’; } This problem tests your knowledge of how type juggling, or coercion works. When a string and a number are compared with the equality operator, the string is coerced
and, it’s not what you expected
$x = true and false; var_dump($x); This one is interesting. In PHP, you can use && or and to do a logical comparison. However, due to the Operator Precedence rules, and actually has lower precedence than =, where && has higher!
Testing your knowledge of array_merge
$arr = array(); $arr[‘val1’] = array(1, 2); $arr[‘val2’] = 3; $arr[‘val3’] = array(4, 5); $arr2 = array(); $arr2 = array_merge($arr2, $arr[‘val1’]); var_dump($arr2); $arr2 = array_merge($arr2, $arr[‘val2’]); var_dump($arr2); $arr2 = array_merge($arr2, $arr[‘val3′]); var_dump($arr2); There’s a problem with this code. It lies
Watch out for that Octal!
var_dump(0123 == 123); var_dump(‘0123’ == 123); var_dump(‘0123’ === 123); At first glance one might think that the leading zero on line one is simply of no value. But one would be wrong… In PHP, a leading zero in a number
strpos gotcha!
$string1 = ‘php is fun!’; $string2 = ‘php’; if (strpos($string1,$string2)) { echo “\”” . $string1 . “\” contains \”” . $string2 . “\””; } else { echo “\”” . $string1 . “\” does not contain \”” . $string2 . “\””;
Post-Fix Increment Problem with Solution
Today I saw an interesting problem. At first glance I wasn’t sure what I was looking at. It took me a moment to parse it. Here is my own version of the problem: $x = 3; echo $x; echo “<br
Imperative vs Declarative (Why OOP is good)
Imperative Programming focuses on how a program operates. Declarative programing focuses on what the program should accomplish without specifying how it will accomplish it. In a way, the Abstraction of Object Oriented Programming can be viewed as a more Declarative