Jump to content

Beginning To Learn Php


Recommended Posts

Some Notes on Variables

 

The first thing you should know about variables is that they are all denoted with a dollar sign at the beginning. For example:

 

>$IAmAVariable
 IAmNotAVariable

 

As mentioned in a previous lesson, all variables are CASE SENSITIVE. A variable may be composed of letters, digits (may NOT be the first character following the dollar sign), and the underscore character ('_').

 

Variable assignment is very easy. It is always written like this:

>$VariableName="whatever";               //quotes should be used with text strings

or

>$5=3+2;                                  //quotes aren't required for mathematical expressions

 

If you re-assign a variable, the second assignment is effectively nullified...it will have no effect. Also, assigning a variable in one file DOES NOT affect variables of the same name in another file. Assigning a variable in one file DOES persist throughout the execution of that file, no matter how many times you've changed from PHP to html to PHP.

 

 

Using the ECHO function

 

ECHO is one of the two most basic functions for printing to output. You can use it with or without parenthesis. However, there is one difference between the two methods. Try the following examples one at a time:

 

><?php
echo "This will print just fine!";
?>

and

><?php
echo ("This will also print just fine!");
?>

 

The difference is the ability to use multiple arguments in the version without parenthesis:

 

><?php
echo "Without parenthesis", "you can use a comma to create multiple arguments.";
?>

and

><?
echo ("If you do that when you use parenthesis", "you will get a PARSE error.");
?>

 

 

Using the PRINT function

 

PRINT is the other most basic function for printing to output. You can NOT use multiple arguments with this function like you can with ECHO. However, the PRINT function returns a value indicating whether or not evaluation of the statement succeeded (value of 1 if it succeeded and 0 if it did not). "It is rare that a syntactically correct PRINT statement will fail, but in theory this provides a means to test, for example, if the user's browser has closed the connection." You can use both PRINT and ECHO to successfully print numbers and text strings.

 

 

NOW...LET'S THROW IT ALL TOGETHER. Here's several examples to show how variables can be used with or without PRINT and ECHO functions. Remember...variables are case sensitive, functions are not.

 

1. Simple output of a variable (also try replacing PRINT with ECHO...you'll get the same output):

><?php  
$myname="Tracy";
print "$myname";
?>

 

2. Now let's include output of a variable within a string of text. Also you'll note the use of the <'br'> tag to render a line break:

><?php
$myname="Tracy";
$myage="31";
$career1="photographer";
$career2="web designer";
$career3="medical lab tech";
$career4="Air Force Reservist";
print "My name is $myname and I am $myage.<br>";
echo "My careers of choice include:  $career1, $career2, $career3, and $career4."
?>

 

I'm out of time this morning. Next will be a short lesson on one other aspect of variables. All above information

is credited to the PHP 4 Bible by Tim Converse and Joyce Park.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...