iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (35)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

July 3, 2008
Poll: Which Web editor do you use?
About
 
July 3, 2008
Book Review: Head First JavaScript
WebReference.com
 
July 3, 2008
10 Things You Can Do With a Wiki
About
 
July 2, 2008
Web Host Reviews - 10 New Reviews
About
 
July 2, 2008
14 Reasons You Should Join a Social Network
About
 
July 1, 2008
Mark Boulton's Freelance Design Secrets
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /PHP /Introduction to PHP

PHP Lesson Two - Variables 

  Views:    26498
  Votes:    4
by James N Hitz 1/23/04 Rating: 

Synopsis:

Lesson 2: takes you into the realm of PHP variables, how they are defined, concatenation and PHP operators.
Pages: firstback11 forwardlast
The Article

String Variables

Let's jog your memory:

"a variable is a named memory location which can be assigned a value"

We said this (or something close to this) in our previous discussion of variables. A variable therefore contains only one value. If this value is enclosed in double quotes it is a "string". Therefore if you had the following code snippet:

$myname="James";
$myname="Hitz";

The value of $myname at the end of the code execution is going to be Hitz. This is because by assigning a different value to an existing variable, you erase the previously stored value and replace it with the new one.

What if you wanted to add 'James' to 'Hitz' to get 'James Hitz'? Enter concatenation to the Rescure.
String Concatenation

String Concatenation means combining two or more string to get one compound string. Look at this:

$FirstName="James";
$LastName="Hitz";
$fullName=$firstName + $LastName;

It is WRONG! This is because of the mere fact that you are trying to deal mathematically with strings. $FullName now has the value'0'. Yes Zero!

"But it works in JavaScript!"

Yeah. Sorry buddy but this ain't JavaScript. In PHP, you concatenate using the dot operator (.) and not the plus operator(+). This works like this:

$firstName="James";
$lastName="Hitz";
$fullName=$firstName.$lastName;

Voila! we got it! We got it!

Shh.Did we really? This code will give us 'JamesHitz'. This is one Un-pronounable name that would cause the most eloquent broadcaster to resign! :-)

We need to add a space between the 2 names for two reasons:
We wanted 2 names in the first place
To save that poor guy from having to resign... <tee hee>

Solution:

$lastName = "Hitz";
$firstName = "James";
$fullName = $firstName . " " . $lastName;

This means: "$lastName is 'Hitz' and the $firstName is 'James' therefore the $fullName is 'James', plus a space (notice it is in double quotes), plus 'Hitz' to produce something more pronounable (James Hitz) and to save a Job"

Well... Thank you very much for being with me thus far. This is then end of Lesson 2 and an advent into Lesson 3. See you there!

Pages: firstback11 forwardlast

Similar/related articles:


 
  Sponsors