ServersWelcome to the World of PHP Page 5

Welcome to the World of PHP Page 5

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




Scalar Variables
and Data

In most cases, PHP scripts will work with data — to process, manipulate, and output. Data generally consists of nu
mbers (numeric values) and letters or words (string values), although there are other types of data that PHP understands.

  • Integers and floating-point numbers are the two numeric data types in PHP.

    An integer is any whole number, such as 5,
    10, 0, or -3.
    A floating-point number is any number with a decimal portion, such as 5.25 or -43.2339.

  • Strin
    g data is composed of any alphanumeric characters, such as words or symbols. Examples of strings include “pepsi”, “Th
    e Simpsons”, “23 skidoo”, “Yahoo!”, and so on.

Integer, floating-point, and string values are
all known as scalar data because they are single data points. This is another way of saying that the number 5, or the word &
quot;hockey”, is just “one thing” (as opposed to, say, a list of things which we will see shortly).

In PHP, as
with most programming languages, we use variables to contain a value — a variable is like a label that refers to a value. The valu
e assigned to a variable can change over time, as a result of processing and manipulation such as arithmetic. Variable names are usu
ally words and are preceded with a dollar sign ($) in PHP. Let”s assign the value 100 to a variable named $price:

$
price = 100;

Code snippets such as the above are examples, but remember that PHP code in a page must always appear within &
lt;?php … ?> tags, even though we will not always write those tags around every single example snippet.

Also notice tha
t good practice suggests we end PHP statements with a semicolon, as seen above. A statement is sort of like an English senten
ce — it is one full programming “thought”. PHP statements are made up of expressions, where an expression is a mea
ningful fragment of code. The previous example represents an assignment expression, because a value has been assigned to a va
riable. In this case, the statement happens to be made up of only the one expression, which is perfectly fine. Many PHP statements a
re made up of several expressions, which we”ll see later.

PHP determines the data type of a variable based on context, such
as the context of the assignment expression:

$price = 100; $firstName = "Martin"; $breed = ''ferret''; $name = $f
irstName;

In the first line above, which we have already seen, PHP assumes that 100 is an integer. In the second lin
e, PHP understands Martin as a string because it is enclosed in double quotation marks. The same applies to ferret, al
though enclosed in single quotation marks. In the fourth example, $name is assigned whatever value is currently held by $f
irstName
, which happens to be a string, so $name also contains a string value (Martin).

Note that in the fo
urth example, $name receives a copy of the value in $firstName. Thus, if we then change the value of $name, thi
s has no effect on the value held by $firstName.

Important:
Variable names in PHP are case-sensitive. In other words, the variable $firstName is completely independent from a var
iable named $firstname.

Why does a value”s data type matter to PHP, or to us for that matter? It matters when
you need to perform an operation on one or more values. Operations include arithmetic, such as addition and subtraction. For
example, you can add an integer and a floating-point value to yield another floating-point value:

$total = $price + 3.95; #
flat rate shipping

Above, $total will contain the value of whatever $price contained plus 3.95. Also notice t
he use of a comment marked by a pound sign (#) — PHP will ignore everything from the pound sign to the end of that line, so you can
add annotations to clarify the purpose of a line of code.

But suppose you tried to add an integer and a string value?

$total = $price + “fruit juice”;

Of course that doesn”t make any sense! PHP will try to resolve this problem
by converting a string to a number, if the string looks like a number such as “4.50” (converts to 4.50) or even “10 a
pples” (converts to 10). In the above example, PHP can”t make any number out of “fruit juice”, so it will be given t
he value 0, and 0 is what will be added to $price.

Variables are a crucial component of most programming languages in
cluding PHP — you will use them to store pieces of data, manipulate the data, and usually output some of the data.


NEXT ->

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories