PHP syntax
Takeaway: Use this information to become familiar with PHP syntax.
By David Sklar and Adam Trachtenberg
PHP's basic syntax is familiar.
produces
Variables are marked with a preceding $. You could write "Hello, World!" like this:
String concatenation is done with . (a period); other arithmetic operators are what you would expect:
produces
PHP has the full complement of operators, and they work just as you'd expect them to--especially if you come from a background of C or C++. A good rule of thumb for PHP: "When in doubt, try it; it will probably work."
Just as in Perl, a string surrounded with double quotes causes variables inside it to be interpolated, but a string surrounded with single quotes does not. So,
produces
Note that the \n in the string turns into a new line, just as in Perl or in C. This only works in double-quoted strings, however.
<?php
echo "Hello, World!";
?>
Hello, World!
<?php
$message = "Hello, World!";
echo $message;
?>
<?php
$greeting = "Hello ";
$num = 3 + 2;
$num++;
echo "$greeting $num people!";
?> Hello 6 people!
<?php
$name = 'Susannah';
$greeting_1 = "Hello, $name!";
$greeting_2 = 'Hello, $name!';
echo "$greeting_1\n";
echo "$greeting_2\n";
?> Hello, Susannah!
Hello, $name!
Variables
PHP makes environment variables available to you as regular variables. This includes the environment variables that are set by the server for a CGI program (even if you're running PHP as a module). For this reason, if the page "http://www.domain.com/farm/cattle/cow-cow.cow.html" contains the code
<?php
echo "[$REQUEST_URI]";
?>
it prints out [/farm/cattle/cow-cow-cow.html]
Arrays
You set off array indices (regular or associative) with square brackets ([ and ]):
$fruit[0] = 'banana';
$fruit[1] = 'papaya';
$favorites['animal'] = 'turtle';
$favorites['monster'] = 'cookie';
If you assign something to an array but leave the index blank, PHP assigns the object onto the end of the array. The statements about $fruit, above, produce the same result as:
$fruit[] = 'banana';
$fruit[] = 'papaya';
You can have multidimensional arrays, too:
$people['David']['shirt'] = 'blue';
$people['David']['car'] = 'minivan';
$people['Adam']['shirt'] = 'white';
$people['Adam']['car'] = 'sedan';
A shortcut for creating arrays is the array()
function:
$fruit = array('banana','papaya');
$favorites = array('animal' => 'turtle',
'monster' => 'cookie);
or
$people = array ('David' => array('shirt' => 'blue',
'car' => 'minivan'),
'Adam' => array('shirt' => 'white',
'car' => 'sedan'));
The built-in function count() tells you how many elements are in an array:
$fruit = array('banana','papaya');
print count($fruit);
prints
2
Control Structures
You can use looping structures such as for and while:
for ($i = 4; $i < 8; $i++) {
print "I have eaten $i bagels today.\n";
}
prints
I have eaten 4 bagels today.
I have eaten 5 bagels today.
I have eaten 6 bagels today.
I have eaten 7 bagels today.
So does
$i = 4;
while ($i < 8) {
print "I have eaten $i bagels today.\n";
$i++;
}
You can use the control structures if and elseif:
if ($user_count > 200) {
print "The site is busy right now!";
} elseif ($user_count > 100) {
print "The site is sort of active right now!";
else {
print "The site is lonely - only $user_count user logged on.";
}
The rule of thumb about operators also applies to control structures. You can use switch, do...while, and even the ?: construct.
David Sklar is the CTO of Student.Net Publishing.
Adam Trachtenberg is the Vice President for Production of Student.Net Publishing.
SponsoredWhite Papers, Webcasts, and Downloads
- The Case for Virtual Local Area Networks (VLANs) Global Knowledge
- 7 Things Every System Administrator Should Know About OpenSSH Global Knowledge
- BitLocker: Is It Really Secure? Global Knowledge
- Geek-Speak Glossary: A Manager's Guide to IT Terminology Global Knowledge
- Preparing for and Taking the PMP Certification Exam Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
