Code2Design.com

User login

Programming

The Layout

Navigation

Popular content

Resources

Who's online

There are currently 2 users and 11 guests online.

Online users

  • DyemAquamma
  • Maryonne

Lesson 2 - PHP Code

Watch the Movie of Lesson 2

If you visited the link in the last lesson you probably already know how to tell PHP code and how to comment it. Never the less, I want to make sure and go over it just so that no one is confused.

This is what PHP code looks like by itself:

<?php
 
...(more code)....
 ...(
more code)....
 echo 
"$$var dollars spent.";
 ...(
more code)....
 ...(
more code)....
?>

Everything between the opening "<?php" & "?>" tags is sent to the PHP core to be processed, then the result is sent back. and the rest of the page continues to load. Here is what PHP code might look like in an HTML file:

<HTML>
<HEAD>
<TITLE><?php echo $tile?><TITLE>
</HEAD>
<BODY>
<?php echo "Welcome to $sitename"?>
<p>We are committed to serving you!</p>
</BODY>
</HTML>

Don't worry if you don't understand it, this is just so you know what to expect.

Another important part of programming is to document your code. Imagine trying to find a function or variable in a 1000 line PHP document! Without any human comments in the file it could take you forever to find what you were looking for. Here are the two most common forms of placing "Human Readable" text in a program:

<?php
// two forward slashes tell the program to ignore what is behind them on the line.
?>

<?php
/*
This is how you "comment" multiple lines in something.
Everything between the opening "/*" and closing "*/
" is
Ignored by the program. This form is more common than the
"
//" (Double-backslash) because unlike the other form this
one is not limited to only ONE line of text.
*/
?>

The text in both of these "comments" is ignored by PHP. Here is an example of using them in a script:

<?php
/* CONFIGURATION of connect to DATABASE SECTION*/
$servername='localhost' // Replace 'localhost' with your server name
$database_username='root'// Replace 'root' with your username
$database_password='password'// Replace 'password' with your password
$database_name='database_1'// Replace 'database_1' with your database name

/* take all of the values and connect to the database */
mysql_connect($servername,$database_username,$database_password);
mysql_select_db($database_name);
?>

See how the comments help to describe what to do here? While you probably don't know what these functions are; you can get a general idea of what this script does thanks to comments in the code.

One thing you need to understand before we go any further is the concept of "variables". (If you are a programmer you probably already have a knowledge of variables. Therefore you are welcome to skip this intro.)

Wikipedia.org describes a variable as: "...a place to store a value in computer memory." A variable is stored in a "byte" (or collection of bytes) in the computers memory. Variables store some value like a phone number or age in the bytes. Most computers now-a-day have about 256MB (Mega-bytes of memory) which is 256,000,000 bytes (Mega means "million").

If I have lost you I apologize. Maybe you can understand variables better if I relate them to P.O. Boxes at the post office. There are many different P.O. boxes (Think "variables")in your post office. At any time they could be used to store many different pieces of information - your paycheck, a letter from grandma, a warrant of arrest for that guy down the street...ANYTHING. Therefore they are like variables, they store different pieces of information and what they hold can change from day to day.
Here is what a variable in PHP looks like:
Example:

<?php
$variablename
?>

Right now there is nothing in the variable "$variablename". So lets add something..
Example:

<?php
$variablename 
'John';
?>

Now the variable "$variablename" holds the value "John". Think of it like this: If you opened up "$variablename" you would find the word "John" inside! (Just like PO Box "204" holds a postcard.) "Capitalization is very important in PHP. It is generally a good idea to have all variables be entirely lowercase, and only use alphanumeric characters along with underscores (_) if necessary. $greeting is not the same as $Greeting, so it is important to remain consistent and watch the typos." -seretogis Below is a list of links for you to follow so that you can learn the syntax of the PHP variables and how to use them. These are the best tutorials I have found so have fun and see you in lesson 3!

Melonfire - Intro & Variables

free2code - "Variables"

PHPFreaks - Variables


Submitted by David on November 9, 2006 - 5:46pm.
printer friendly version

These tutorials are great,

These tutorials are great, thanks so much! I've been doing HTML for a few years and CSS for a few months, and this is perfect for finally getting me into PHP!


SciTE Editor?

In this video, David says you can download a slighty modified version of the SciTE Editor from Code2Design, but I can't seem to find the link. Could someome please give me the link please? (via PM, comment, or Email)

Thanks :)
- kab012345


Outdated

I still used a custom v1.59 which was hard to find back then. However, I recommend the newest version 1.75 now. Download


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati