Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Variable-typing in PHP

Tuesday, April 16th, 2019 by Servage

php-explorer-tab-search

PHP is a very loosely typed language. This means that variables do not have to be declared before they are used, and that PHP always converts variables to the type required by their context when they are accessed.

For example, you can create a multiple-digit number and extract the “nth” digit from it, simply by assuming it to be a string. In the following snippet of code the numbers 12345 and 67890 are multiplied together, returning a result of 838102050, which is then placed in the variable $number.

Automatic conversion from a number to a string

<?php
$number = 12345 * 67890;
echo substr($number, 3, 1);
?>

At the point of the assignment, $number is a numeric variable. But on the second line, a call is placed to the PHP function substr, which asks for one character to be returned from $number, starting at the fourth position (remembering that PHP offsets start from zero). To do this, PHP turns $number into a nine-character string, so that substr can access it and return the character, which in this case is 1.

The same goes for turning a string into a number, and so on. In Example 3-11, the variable $pi is set to a string value, which is then automatically turned into a floating point number in the third line by the equation for calculating a circle’s area, which outputs the value 78.5398175.

Automatically converting a string to a number

<?php
$pi = "3.1415927";
$radius = 5;
echo $pi * ($radius * $radius);
?>

In practice, what this all means is that you don’t have to worry too much about your variable types. Just assign them values that make sense to you, and PHP will convert them if necessary. Then, when you want to retrieve values, just ask for them—for example, with an echo statement.

Constants

Constants are similar to variables, holding information to be accessed later, except that they are what they sound like—constant. In other words, once you have defined one, its value is set for the remainder of the program and cannot be altered.

One example of a use for a constant might be to hold the location of your server root (the folder containing the main files of your website). You would define such a constant like this:

define("ROOT_LOCATION", "/usr/local/www/");

Then, to read the contents of the variable, you just refer to it like a regular variable (but it isn’t preceded by a dollar sign):

$directory = ROOT_LOCATION;

Now, whenever you need to run your PHP code on a different server with a different folder configuration, you have only a single line of code to change.

The main two things you have to remember about constants are that they must not be prefaced with a $ (as with regular variables), and that you can define them only using the define function.

It is generally agreed to be good practice to use only uppercase for constant variable names, especially if other people will also read your code.

Predefined constants

PHP comes ready-made with dozens of predefined constants that you most probably will be unlikely to use as a beginner. However, there are a few, known as the magic constants, that you will find useful right from the start. The names of the magic constants always have two underscores at the beginning and two at the end, so that you won’t accidentally try to name one of your own constants with a name that is already taken.

Sources for further reading

Variable-typing in PHP, 4.0 out of 5 based on 3 ratings
Categories: Guides & Tutorials, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.