Anonymous Functions Speak Volumes in Code Anonymous functions, or closures, are functions that seem to cause a lot of stress in people’s lives. Let’s simplify this process. An anonymous function is a function without a name. Let’s look at a regular function. A regular function starts with the function keyword and is followed by the function name; the parameters are enclosed inside of parentheses and the function body is enclosed inside of curly braces. <?php function function_name( $parameter_name ) { // body } ?> The anonymous function drops the function name and assigns the function to a variable. <?php $var_name = function ( $parameter_name ) {

Adapting and Performing with Variable Functions PHP supports the concept of variable functions. What that means is that you can append parentheses to a variable and use it as a function call. Although not frequently used, it can be beneficial when you want to call a function dynamically based on the value stored inside of the variable. Let’s just create some examples and see that this is pretty straight forward. We’ll start by creating two functions: subaru() and nissan(). The two functions will echo out different strings. We’ll then create a $car variable and assign a string to it that matches one of the function names: either subaru or nissan. To call

Navigating Code Waters for Treasured Returns Functions can return values: there I said it. When we looked at functions previously, those functions just echoed some string. Although that is one use of a function, functions are more powerful than simply echoing out statements. We can use functions to do something and then return a calculated value to us. That value can then be echoed out or assigned to a variable. To return a value, you will use the return keyword inside of your function. Make sure that the return statement is the last statement inside of your function since PHP will exit the function after the return statement is executed. Let’s look at a

Transforming Data with the Magic of Parameters If you haven’t read my introduction to user-defined functions article, make sure to do that before continuing on. If you understand what functions are, keep going. Let’s revisit the automotive plant that we looked at in the previous article. Think about an automotive plant: You bring the materials to the plant (you are here). You feed the plant with those materials (and here). The plant takes the materials and somehow produces a car. The automotive plant is the function that can be invoked to produce something. The plant() function that was created before had a local variable $type_of_plant that stored the

Empowering Coders to Shape Logic their Way Functions can be a difficult topic to grasp if you’re a new programmer. I’m sure you were exposed to functions in math, but do you really understand what the term “function” means? Think about an automotive plant: You bring the materials to the plant. You feed the plant with those materials. The plant takes the materials and somehow produces a car (we’ll be focusing on this one). The plant itself is the  function . If you were an engineer, you would know what machinery needs to go inside of the plant in order

Conducting Loops in Harmony with Continue We looked at the break statement in the previous article. As a refresher, the break statement terminates the loop once it’s encountered. Sometimes we want to just skip an iteration and continue with the loop; that can be achieved with the continue statement. Let’s look at how we can use the  continue statement inside of each loop:  while ,  do-while ,  for , and  foreach . Each loop will iterate through the following array: ?php $next_car = [ “Porsche 911”, “Ferrari F355”, “Mitsubishi EVO”, “R34 Skyline”, “C8 Corvette” ]; ?> In the first example, we’ll look at the while loop. PHP will iterate

Loop Liberator: Harnessing Breaks for Precision and Progress We looked at the  break statement when we covered  switch statements. In that case, the break statement was used to end the execution of the switch statement. The break statement isn’t exclusive to the switch statement: it can be used in loops as well. Let’s look at how the break statement ends execution in each of the loops:  while ,  do-while ,  for , and  foreach . We’ll start off by creating an array that the loops will iterate through. <?php $favorite_programming_language = [ “java”, “javascript”, “php”, “c”, “c#”, “c++” ]; ?> The goal of each loop will be to iterate through the $favorite_programming_language array and

Unleash the Power of Each: Embrace the Foreach! You can use the  for loop to iterate through an array, but to really shine, you need to use the  foreach loop. The foreach loop has the following syntax. foreach ($array as $key => $value) You’ll read the structure like this: For each array as key/value … or For each key/value pair inside of an array. It means that the foreach loop will loop through each array element and you will have access to each key/value pair of each element. This is great if you’re dealing with associative arrays. If the key is a string, you will not be able

Data is a critical asset for any organization, and managing it effectively is essential for driving business value and achieving competitive advantage. As such, CIOs need to play a key role in ensuring that their organizations are managing data effectively. This article highlights some of the things that CIOs need to do with data. Develop a data management strategy The first step for any CIO in managing data effectively is to develop a data management strategy. This strategy should outline how the organization will manage data, including how it will be collected, stored, secured, analyzed, and used. The strategy should

Loop it up: Unleash the power of for loops! I use for loops way more than I use  while loops. It seems like most of the time I’m reading items from a database:  for loops are perfect for that. I tend to use while loops in Java programs where I need to execute some code until the program terminates; that’s not realistic in PHP. In web-programming, I almost exclusively use for and foreach loops. In most programming languages, the for loop follows the following syntax: for (exp_before; exp_before_loop_body; exp_after) { loop_body } Once PHP encounters the for loop, it looks at the expression exp_before to initialize the necessary variables that are required by the loop. When