PHP Tutorial: Scalars and Arrays

Many webmasters fail to learn PHP, or fail to try to learn PHP, because the terminology seems very complicated and technical. Scalars and arrays are a good example of this. Their names may seem technical and specific, but if you strip these down to just their concepts, they are relatively easy to understand. We will attempt to explain these to you here and show you how to use these in PHP.

Scalars – What They Are

Before we get into what scalars are, it might be good to give an example of what a scalar is. Below is a bit of PHP code that uses a scalar.

<?php
$variable = “This is a scalar”;
echo $variable;
?>


And there you have it. Scalars are really just variables, and the most basic type of variables out there. In PHP, and programming in general, a variable is a structure that holds data and is uniquely named by the programmer. The programmer may name the variable anything they wish, the result will be the same. In PHP, you designate something as a variable by placing the ‘$’ in front of it.

Consider the previous example. In that example we told PHP that “$variable” was equal to “This is a scalar”. Now, anytime you tell PHP to do something with $variable, it will do it as if you typed in “This is a scalar” instead of $variable. The following two scripts try to demonstrate this:

Example 1.
<?php
$variable = “This is a scalar”;
echo $variable;
?>


Example 2.
<?php
echo “This is a scalar”;
?>


Both example 1 and example 2 will create the same result. PHP will spit out “This is a scalar”.

When writing your PHP code, make sure that you do not use your variable names more than once. If you do, you will overwrite the information that you originally stored to the variable. For example:

<?php
$v = “This is my original content”;

// Some other PHP code

$v = “This is some other content”;

echo $v;
?>


In this example, PHP will forget that you ever set $v to equal “This is my original content” and go simply with the new value you assigned to $v.

Tip: PHP allows you to “add on” to a scalar very easily. To do this, try the following example:

Article Tip
<?php
$variable = “This is the first part ”;
$variable .= “This is the second part”;

echo $variable;
?>


This will output:

This is the first part This is the second part

If you want to add on to a scalar, simply add a period symbol in front of the equals sign.


When setting your variable names, you can use any name you want. PHP does pay attention to capitalization, so be mindful of that. In other words, $variable will not be the same thing as $Variable.

Now let’s try an example using different variables.

<?php

$firstname = “Louis”;
$lastname = “Dreyfus”;
$address = “123 Main Street”;

$sentence = “$firstname $lastname lives at $address”;

echo $sentence;
?>


This should output:

Louis Dreyfus lives at 123 Main Street

Questions about scalars? Ask them in our PHP Forums

Arrays

Arrays are another basic concept that sounds just a bit more difficult to understand than it really is. The concept of arrays is something that you should be able to understand very easily, especially if you understood scalars. The reason for this is that arrays are also variables, but with a little wrinkle.

Scalars, as we saw earlier, are devices that hold a single piece of information for later use. So if you set $name = “Craig” anytime you invoke $name PHP will spit out “Craig”. Arrays are the same thing, except they can hold more than one piece of information. Below is an example of an array and how you make one in PHP:

<?php

$contact = array(“First Name”,”Last Name”,”Address”);

?>


In this example, the beginning part looks very much like a regular scalar. We have the familiar “$” symbol, we have the name of the variable as defined by the programmer (in this example this is ‘contact’), and we have something that it is equal to. However, things change after that. First, we type out the word “array”. This tells PHP that we are not setting a scalar, but rather we are making $contact into an array of information. Next we have an open parenthesis. Whatever happens inside the parentheses is intended to become a part of the array.

The next part is where the meat of setting the array takes place. In this array we are setting three pieces of information: First Name, Last Name, and Address. So when we talk about $contact, we can say that it is an array that holds three elements.

So just how do you access the information in the array? Using echo like we did with scalars will not work. Go ahead and try it. Use the following snippet as an example:

<?php
$contact = array(“First Name”,”Last Name”,”Address”);
echo $contact;
?>


It did not work out like the last ones, did it? When PHP is asked to echo a variable that is an array it will simply tell you that it is an array, just like it did here. The reason for this is that you need to tell PHP which element in the array you want it to spit out. To do this, all you need to do is count. Try this example:

<?php
$contact = array(“First Name”,”Last Name”,”Address”);
echo $contact[1];
?>


You should have seen it spit out “Last Name”. Remember how earlier we said that $contact was an array with three elements? Well, in order for PHP to spit out some information, we need to tell it which element we want it to work with. So, let’s count it out using numbers to identify the elements in the array.

<?php
$contact = array(“First Name”,”Last Name”,”Address”);
echo $contact[0];
echo $contact[1];
echo $contact[2];
?>


There we have it, PHP has now spit out all three elements of our array. It is good to note at this point that PHP arrays always starts with “0” when numbering the elements of an array.

Article Tip
If you ever want to see all the elements of an array spit out for troubleshooting purposes, use the following command:

<?php

print_r($array);

?>


Make sure you replace “$array” with the actual name of your array.


So to review, an array is a type of variable. However, instead of holding one value, an array can hold multiple values. In other words, an array acts as a variable that can hold multiple scalars. Below are three ways to set an array:

<?php

// Setting an Array – Three Methods
// Method 1

$contact = array(“First Name”,”Last Name”,”Address”);

// Method 2

$firstname = “First Name”;
$lastname = “Last Name”;
$address = “Address”;
$contact = array($firstname,$lastname,$address);

// Method 3

$contact[0] = “First Name”;
$contact[1] = “Last Name”;
$contact[3] = “Address”;

?>


All three methods set the exact same array. All three methods are valid ways of setting a proper array.

Arrays play a role in nearly every PHP script that you will ever write, so it is important that you spend some time understanding arrays. As with all things, the more time you spend working with arrays, the easier they will become.

Conclusion

Scalars and arrays may sound like very technical aspects of PHP programming, but when they are reduced to their core, they become relatively simple concepts. Both scalars and arrays are nothing more than variables, a way of storing information within a script so you can use it over and over again without having to write all the information over again.

Share This Articles:
Rate This Article:
 
AddThis Social Bookmark Button
Not so Great Article >> 1 2 3 4 5 << Great Article
 
Social BookMark it!:
Your feedback is appreciated! Please take the time to rate this article.
 
About this author

Mark Daoust is the owner of http://www.site-reference.com. Discuss this article futher at http://forums.site-reference.com/

 
Related articles

A Shopping Cart Class in PHP
Classes have eluded me (rather I have eluded them) since the beginning of my primitive programming days (remember Turbo C++?). They have always hovered over the horizons. This hasn't nagged me much, for I've been managing without classes pretty well, but sooner or later I had to come in contact with them. So in the morning (and I hadn't slept the entire night) I executed a gut-wrenching plunge into the depths of classes, and the shopping cart class was the first thing I wrote. Although I have made content management systems and algorithmic quote generations in PHP, I had not added a shopping cart class code to my code repository.

PHP Tutorial: The Very Basics
Many website owners believe that learning PHP is simply beyond them and should be left to the technical people. What they do not know is that PHP is actually very simple to understand and learn, and that with just a little practice, a willingness to learn and to experiment, they can become fairly proficient at PHP and incorporate the many benefits it brings into their websites.


Warning: fopen(cache/PHP-Tutorial-Scalars-and-Arrays.html) [function.fopen]: failed to open stream: Permission denied in /home/site/public_html/viewArticle.php on line 141

Warning: fwrite(): supplied argument is not a valid stream resource in /home/site/public_html/viewArticle.php on line 142

Warning: fclose(): supplied argument is not a valid stream resource in /home/site/public_html/viewArticle.php on line 143