Page 1 of 1

Introduction: Scalar Variables Introduction and Scalar Variables Rate Topic: -----

#1 niallj  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 05

Posted 29 January 2005 - 03:53 PM

PHP Basics :: Introduction and Basics

PHP is a language used to make HTML more dynamic. Contrary to Javascript, it modifies the HTML of a page before it leaves the server- the client browser sees only HTML code. It can be used for creating log in systems, forums, content management systems, as well as a multitude of other things. Other tutorials here may focus on getting a specific task done, but at the core of any language is syntax that must be learned and understood if you are ever to progress in it. This is what I hope to explain to you about the PHP language.

PHP can be integrated directly with HTML. A page can be written entirely in HTML, with a small PHP command at the very bottom to print the date, or it can be written entirely in PHP, with PHP outputting the necessary HTML tags. PHP files take the extension ".php". It is important to note that you must have a webserver installed on your machine to test your PHP- it cannot be simply opened in IE or Firefox, as HTML can. If you don;t already have a webserver, I reccomend the excellent Apache2Triad.

With the beginnings out of the way, let's get down to it. :)

PHP code is marked by the <?php?> tag. The opening tag takes the form <?php, followed by the code, followed by ?>, like so:

<html>
<head>
<title>Boo!</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body>
</HTML>



Another point to mention before we go into the world of variables is that certain statements in PHP must be followed by a semi colon. Things such as function definition opening and closing statements, loop opening and closing statements and class opening statements (Don't panic- I'll explain what these are in a later tutorial) do not require semi colons. Things such as variable definitions, echo statements and function calls do. Although you may not understand what the code below does, it's useful to see which statements require the colon and which do not.
<?php
function printStuff(){ //function definition- no semi colon
	echo "Stuff!"; //echoing statement- semi colon 
} //end of function definition- no semi colon
while(1){ 
	//loop opening- no semi colon
	echo "Looping!"; //echoing statement- semi colon
} //end of echo statement- no semi colon
?>



You get the idea I'm sure, and if not don't worry- I'll explain which elements require semi colons and which do not as we reach the point of using them. :)

Finally- before we get onto something interesting ;), PHP code can be "commented", with the // /**/ or # operators. Comments are designed to leave notes in your code, making it easier to read. This means, that if you see such a thing in my code, it is probably a helpful note on the code, for you to read. If you like ;).

VARIABLES

Any programming language must have the facility to store data- the language would be totally impractical otherwise. Data can be stored in memory, in a database or in a file but in all three cases the program needs somewhere to store this data while it works with it. These locations are called variables.

Scalar Variables

These variables can hold a range of values in PHP. They can hold numbers (integers and floating points), strings ("Hello World!"), boolean values (true or false), or resource identifiers, which I will explain in greater detail later. These types are often referred to simply as string, int, float and bool.

Scalars are designated by a $ immediately preceeding their name, and can have values assigned or modified with the = operator.

<?php
$anInt= 7; //create a varibale called anInt, and make it equal to 7.
$aFloat= 3.141; //create a variable called aFloat, and make it equal to 3.141
$aString= "Hello World!"; //create a variable called aString, and make it equal to Hello World!
$aBool= true; //create a variable called aBool, and make it equal to true.
?>



The observant of you may have noticed that only the string scalar needed quote marks around it. When assigning numbers or built in values such as true and false to a variable, quotes are not required. You may also have noticed that comments do not require a semi colon at the end of the line.

If you have ever programmed in C, C++, Java or the like, you may be wondering why I haven't included any variable declarations or type specifiers. PHP does not require either. if $VariableName does not exist, the engine will simply create it. Just as easily, all type handling is performed by the scripting engine, meaning that objects can change type seamlessly.

If you have never programmed in a language such as the ones above before, saying only that PHP is less complex when it comes to declaring variables will suffice ;)

Before we move onto the next type of variable, it would be very boring if I didn't tell you how to output your information wouldn't it? There are two main ways of doing this, and while I'm at it I'll explain the difference between " and ', as well as showing you another way to put string data into scalars.

But first, outputting data. PHP achieves this using either echo or print, and these statements can take on two forms.
The code snippet below shows them.

<?php
//the echo method
echo "The echo statement as a language structure.";
echo("The echo statement as a function"); //what's a function? :o I'll explain later;)

//the print method
print "The print statement as a language structure";
print("The print statement as a function");
?>



It makes no difference to your code whatsoever which of these you use. I also promise that in a later tutorial, I'll explain the difference between a "function" (echo(...)), and a "language construct" (echo ...).

The above shows outputting of strings, although it's easy to output any of the basic types or a variable. For the tutorial, I'll be using echo, but be aware that print exists. The following example shows outputting various data forms and variables.

<?php
$anInt= 5;
echo $anInt;
$aFloat= 5.67891;
echo $aFloat;
$aString= "Boo!";
echo $aString;
$anotherString= "Boo too!";
echo $anotherString;
?>



Easy eh? I'd like to make a quick note here- a statement such as "echo true;" may not echo true. !. How you say? true and false, (as well as TRUE and FALSE) are language keywords. PHP recognises them with a special meaning, and get this- essentially treats them as 1 and 0. Traditionally in computing logic, 1 represents true and 0 represents 0, and so that is how PHP sees the true and false keywords. That's why if you perform "echo true;" you'll actually see a number 1. Interesting eh?

Another thing that the observant of you may notice- everything is printed on the same line. The above example may print something like "55.67891Boo!Boo too!", but why? Is the great PHP not so infalliable? The answer is that we haven't instructed PHP to add a new line- have you forgotten that HTML ignores whitespace? To make a new line we need to add <br /> to the end of any string. But what if you have a number, in a variable or otherwise? Neither echo 5"<br />"; or $number= 5; $number + "<br />"; will work. The solution is this- if you are outputting a variable, use a dot, like so:

<?php
$number= 5;
echo $number."<br />";
?>



What is this wizardry I hear you ask?? The dot has special significance outside of strings. It is called the concatenation operator. In an echo statement it means, stick the bit before me together with the bit that comes after me and treat it as one variable. Clever, don't you think?

It can also be used to attach two strings outside of an echo statement, like so:

<?php
$firstString= "Hello";
$secondString= " World!";
$thridString= $firstString.$secondString;
echo $thirdString;
?>



If you hadn't already guessed, that script echoes: Hello World!

Notes about " and '

The differences between " and ' are subtle, so I'll leave them until the end of this note. I'll start by saying certain things about strings within strings. What if you want to say, $myVar= "The cat said to me "Hello"";? PHP will cause an error, probably unknown identifier "Hello". Why? Because PHP actually sees the above string like this:

$myVar <-- unknown variable, declare it
= <-- assignment operator, get ready to define $myVar
" <-- a string is coming up, get ready to start setting $myVar
The cat said to me <--- make $myVar equal this string
"<--- end of the string
Hello <--- not recognised. This isn't a scalar or any other type of variable. It hasn't been made with define(), and it isn't a built in keyword... PANIC!
*At this point PHP throws your error*
"" <-- empty string
; <-- statement ends



So how can we make a string inside another string? One way would be to use different types of quotes, like this:

$myVar= 'The cat said to me "Hello!"';



There is no conflict here. The other way is to escape the character. Escaping involves placing a backward slash (\) before a
character to cancel any special meaning.

$myVar= "The cat said to me \"Hello!\"";



The above is perfectly valid, and is necessary when a string within a string is needed. \" is used when " starts the string,
and \' is used when ' starts the string.

And now onto the final part: the difference between " and '.

Using the example that prints different kinds of values with line breaks in between, take a look at the source code. You
should see something like "5<br />5.6789<br />1<br />Boo!<br />Boo too!<br />". It makes fine HTML when rendered, but how can
we make the source more readable? In an echo or print statement, we can use the special character \n to make a line break.
This is not an HTML <br />, merely the equivalent of pressing enter in notepad, or any other text editor. Modify the example so that it looks like
this:

<?php
echo 5;
echo "<br />\n";
echo 5.6789;
echo "<br />\n";
echo true;
echo "<br />\n";
echo "Boo!"."<br />\n";

$outString= "Boo too!";
echo $outstring."<br />\n";
$outNum= 7;
echo $outNum."<br />\n";
?>



Your HTML source output should now look like this:

5<br />
5.6789<br />
1<br />
Boo!<br />
Boo too!<br />

But now replace the double quotes with single quotes. Your output will now look something like this:

5<br />\n5.6789<br />\n1<br />\nBoo!<br />\nBoo too!<br />\n

When using single quotes, escape sequences such as \n are ignored- if you need to use them, a double quote (") is required.

Here endeth the lesson on scalars. Watch out for the next tutorial- "PHP Basics: Arrays"

This post has been edited by niallj: 14 May 2006 - 03:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Introduction: Scalar Variables

#2 niallj  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-January 05

Posted 31 January 2005 - 02:46 PM

Edited 31/1/05 20:44 UTC

Thanks to Amadeus for pointing out my generalisation about the use of semi colons. I apologise to anyone who read that and left wondering why some lines didn't have semi colons. :D

Please if you spot any more mistakes or any of my hideous typos please PM me so I can fix them. Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1