Library tutorials & articles
An Introduction to PHP
Variables & Operators
PHP supports the basic data types of strings, integers, double precision floating
point numbers, etc, but is a weakly typed language. This means that variables
are not tied to a specific data type, and may take any of the data types. Variables
are not declared in PHP, but must be prefixed with a $ sign.
String concatenation is achieved in PHP using the dot (.) operator. The following example uses a variable called anyDataType. The data type is given a string literal, and an integer values. The output is displayed using the dot operator to concatenate a break to each line printed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Using Variables</title>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<p>
<?php
// Use several data types with one variable
$anyDataType = "Hello world";
print $anyDataType . "<br>";
$anyDataType = 64;
print $anyDataType . "<br>";
$anyDataType += 1900;
print $anyDataType . "<br>";
?>
</p>
</body>
</html>
Operators
Arithmetic Operators
| Operator | Description |
+
|
Unary plus, or addition. |
-
|
Unary minus or subtraction. |
*
|
Multiplication. |
/
|
Division. |
%
|
Modulus (remainder from a divide operation). |
++
|
Increment (pre-increment if placed before the variable, post-increment if placed after). |
--
|
Decrement (pre-decrement if placed before the variable, post-decrement if placed after). |
Arithmetic Operator Examples
// Caluclate the sum of a and b
$total = $a + $b;
// Calculate the difference between a and b
$difference = $a - $b;
// Calculate the product of a and b
$product = $a * $b;
// Calculate the quotient of a and b
$quotient = $a / $b;
// Calculate the remainder when a is divided by b
$remainder = $a % $b;
// Increment the value of a
$a++;
// Decrement the value of b
$b--;
// The pre-inrecrement version
$num = ++$a;
// The pre-decrement version
$num = --$b;
Self Assignment
If an assignment is made that requires the value of the variable being assigned to, PHP offers a convenient shortcut by placing the operator immediately before the assignment operator.
$total += $a; // Equivalent to $total = $total + $a;
$difference -= $a; // Equivalent to $difference = $difference - $a;
$product *= $a; // Equivalent to $product = $product * $a;
$quotient /= $a; // Equivalent to $quotient = $quotient / $a;
$remainder %= $a; // Equivalent to $remainder = $remainder % $a;
Relational Operators
Relational operators (sometimes called comparison operators) are used to compare values in an expression. The return value will be either true or false.
PHP Relational Operators
| Operator | Description |
<
|
Less than (eg. $x < 10). |
<=
|
Less than or equal (eg. $x <= 10). |
==
|
Equivalence (eg. $x == 10). |
>
|
Greater than (eg. $x > 10). |
>=
|
Greater than or equal (eg. $x >= 10). |
!=
|
Not equivalent (eg. $x != 10). |
Logical Operators
Logical operators allow you to combine the results of multiple expressions to return a single value that evaluates to either true or false.
PHP Logical Operators
| Operator | Description |
!
|
Logical Not. For example, (!$x) will evaluate to True if $x is zero. |
&&
|
Logical And. For example, ($x > 4 && $y <= 10) will evaluate
to True is $x is greater than four and $y is less than or equal to ten. |
||
|
Logical Or. For example, ($x > 10 || $y > 10) will evaluate to
True is either $x or $y are greater than ten. |
Related articles
Related discussion
-
Create this kind of website
by maidentv (1 replies)
-
PHP London July Meetup
by webdeveloperit (1 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
SOFTWARE PHP WORK
by synctel (0 replies)
-
London PHP / MySQL Superstar needed!
by gatewaytechnolabs (1 replies)
Related podcasts
-
EarthClassMail.com - Moving from LAMP to .NET 3.5
Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.
Events coming up
-
Dec
3
The Auckland PHP December meetup
Auckland, New Zealand
Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.
It is not just talk though: Zend server is flourishing despite the recession - "the Q1 of 2009 has been our strongest quarter ever," says Suraski - and although PHP's enterprise market share is small compared to Java or .NET, it is growing. Read more on ITJOBLOG.
!--removed tag-->but i need to send values in one folder to another
Yes, I think you're correct, although you could easily have a line like this:
to get around that problem. Which I think is what's missing from this tutorial
In the PHP file should it be "$_POST["num"]" ?
instead of just $num?
I have in php a header which brings a save as box , but when file is down loaded it contains the 6 blank spaces at the start of of document .
I have to avoid storing these blank spaces.Please help me to solve this problem.
You can also do string concatenation like
$h = "Hello";
$w = "World";
$message = "$h $w";
This thread is for discussions of An Introduction to PHP.