I’d Like you to Meet my associate. What are PHP Associative Arrays? If you’re familiar with other programming languages, an associative array is pretty much a dictionary. Instead of accessing the array by using an index value, you’ll access it by using a key. That key points to a value. Keys in PHP can be integers or strings. If you attempt to store a floating point number as a key, it will be cast to an integer. Boolean value true will be case to 1 and false will be cast to 0. We can access a regular array with the index value of the
Tag: PHP Programming
Once you understand PHP Arrays, they really do become simple. What is an array? In layman’s terms, it’s a way to store multiple items together under one variable. You could create different variables for everything, but sometimes that’s just not practical. Imagine that you’re pulling thousands of records from a database. You can’t, or shouldn’t, create thousands of different variables to store each record. You would use an array and store all of the records under one variable. You could then loop through the array (I know we haven’t covered loops yet) and display each array element. There are different
What do you use your Strings for? I know that for the past few articles we’ve looked at strings, but we never actually talked about them. A string is just a sequence of characters surrounded by quotes; strings in PHP are placed inside either single or double quotes. In other programming languages, like Java, strings are surrounded by double quotes and individual characters are surrounded by single quotes. <?php $string_1 = ‘This is an acceptable string’; $string_2 = “String with double quotes”; echo $string_1 . “<br>”; echo $string_2 . “<br>”; ?> You don’t have to store PHP Strings
Time to take a dip in the Pool. Where’s my PHP Float? Floats and doubles are the same thing in PHP. The following numbers are considered floating point numbers. <?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ?> PHP Floats have precision for up to 14 digits. That includes the cumulative digits, before and after the period. We’ll create two variables with 14 total digits and one with 15 digits. We’ll add them and see the results that we get. <?php // 14 digits $a = 1.0000000000001; $b = 0.0000000000001; // 15 digits $c =
Integers. You mean math? The integer data type represents whole numbers, either positive or negative or zero, not fractions. Before we get into anything worth looking at, let’s look at a simple example. Create a new PHP file and copy the following code into it. <?php $x = 0; $y = 1; $z = -2; echo $x + $y + $z; ?> You should get a -1 as the result. I know that we haven’t looked at arithmetic operators, but I’m sure that you can figure out what the + operator does in the previous example. We set
What Do You Mean Truthy? We looked at boolean values in the previous article. Booleans represent true or false. PHP Truthy Booleans, which are values that are not true or false, but when evaluated inside an expression, they can act like they are. The best examples are the integers 0 and 1. If we passed 1 to the if statement, PHP would interpret the number 1 as truthy and would execute the statement inside the body. If you pass 0 to the if statement, PHP would interpret 0 as falsy, so it would not execute the statement inside the if
True or False? The PHP boolean value is a scalar data type that represents the values true and false. That’s really it. End of article. That would be a short article so lets take a look at a few examples before we abruptly end it. Create a new file and name it booleans_intro.php. We’ll be testing out the code below with our booleans_intro.php file. One of my favorite vehicles, and this is important for understanding booleans properly, is the MKIV 2JZGTE Toyota Supra. We’ll create a variable to check whether that Supra currently exists in my garage or not.
Can you even store data? The best way to introduce PHP variables is to start thinking about math. You might remember that there are these things called x and y variables that used to store numbers. If x = 2 and y = 5 then what is x + y? Just substitute 2 for x and 5 for y and you get 7. Variables are just storage containers. You can store any data type that you would like inside of them; we’ll look at strings and integers below. In PHP, variables start with a dollar sign, followed by either the
Should you document your code? PHP has a few different options when it comes to comments. Frequently, it’s based on preference on which comment style you use, but other times it makes sense to use one over the other. PHP Comments allow you to write brief or detailed descriptions explaining what the code that you wrote actually does. You, or someone else, will come back to your code years from now and will try to decipher what it actually means. You should always strive to write code that’s self-explanatory, but every once in while, you may want to specify a