LAMP Tutorials
Lamp Tutorials Contact CrackFeed

Introduction to PHP

Part 1 of PHP Basics series

Authored on: June 21, 2011 by Jeffery Dilegge

Digg this tutorial!

Please support Crackfeed with a small donation.

Back to PHP Tutorials

PHP, PHP: Hypertext Preprocessor, is a general purpose scripting language that is used to produce dynamic and interactive applications. PHP works with HTML in that you can run php code inyour static HTML pages to add dynamic features to them. However, PHP code does not ahve to be run within HTML pages and I would not recommend doing so, as that when you run PHP code in an HTML page, anyone can "View Source" and see your PHP source code. This is whay my tutorials never embed PHP within HTML, I do the opposite by embedding my HTML within my PHP code. If you want to see the history and other boring details of PHP, just click here and the Wikipedia page will open in a new window.

Let's get on the ball and create your first PHP application, shall we?

Step One: Open notepad or another plain text editor. DO NOT use a word processor like Microsoft Word and no WYSIWYG apps like Dreamweaver. Just a plain text editor like Notepad or EditPad Pro.

Step Two: Save the document as "test.php" and do not close it.

Step Three: All code must be wrapped inside of PHP tags. So, the first line will always be <?php and the very last line will always be ?>. This tells the PHP process on your server to interpret anything between those tags as PHP and not as static HTML. Your document should look like this at this time:

<?php

?>

Step Four: The most basic of PHP tasks is the echo language construct. Remember that, "language construct". Many people call echo a "function" and that is 100% incorrect. With that aside, back to what it does! echo tells PHP to display whatever is given after echo. For example, I want to say "Hello World" to everyone that views my webpage:

<?php

echo 'Hello World';

?>

You just wrote your first, fully functional PHP script! Save file and run it on your PHP enabled web server.

First off, all commands must end with a semicolon ; to tell php that this is the end of that command. After echo, I put the string "Hello World" within single quotes. You can also use double quotes (aka quotation marks). The difference between the two being that single quotes tell PHP that everything inside of them is literal and not to be processed as code. Using double quotes, tells php that there might be code in there and to process the string for possible code. So, in using single quotes when you are just entering static strings, you make the script faster because the PHP interpretter on your server does not have to searcht he string for possible code, it treats it as plain text instead. Another reason to use single quotes is that if you use double quotes to contain your string and then decide to use double quotes inside of them to quote somebody or similar, you must put a slash \ before the quotation marks or the PHP interpretter will get confused and show an error. Here is an example that would not work:

<?php

echo "Hello World, my name is "Jeff"";

?>

I cannot use double quotes inside double quotes unless I slash them like this:

<?php

echo "Hello World, my name is \"Jeff\"";

?>

However, that takes more time and just looks like crap so since I will most likely use more quoatation marks than I will apostrophies (single quotes), so it is logical to use single quotes when containing my strings to save myself time from not having to slash quotes all of the time, like so:

<?php

echo 'Hello World, my name is "Jeff"';

?>

Wow, faster and better looking too! There is one more reason I never wrap strings in double quotes. PHP gets confused from time to time when you mix strings with variables. An example of what not to do is:

<?php

echo "Hello World, my name is $username";

?>

See the part that says "$username"? That is a variable, it contains a value that you defined previous to using it. The proper and elegant method of doing this would be:

<?php

echo 'Hello World, my name is $username';

?>

But, there is a problem. Remember that anything within single quotes is considered literal and not code? To stop the string and start a variable without confusing yourself or the PHP interpretter, break out a period!

<?php

echo 'Hello World, my name is '.$username;

?>

BAM! I ended the string with a single quote and after the single quote I put a period. The period says to the PHP interpretter, "Hey dude, can you append the value contained within this variable to the end of this string please?"

Now to variables. A variable holds data inside of it. You have to define that data prior to using the variable. Variables so not just create themselves. So, here I will create a variable:

<?php

$myName = 'Jeff';

?>

Now, the variable $myName contains my name, Jeff. So hat can I do with this variable now? A lot! For now, however, I will just echo it:

<?php

$myName = 'Jeff';

echo $myName;

?>

I do not have to put quotes around the variable. The script sees that I just created the variable $myName and assigned it a value. Now I told it to diplay the value of $myName using the echo construct. Then the script will stop becuase it reaches the last line with no additional content or commands. A lot of people put exit; or die; at the end of their scripts, this is silly. It will already exit at the end. You need only add exit or die, which are identical, if you wish to stop the script prematurely based on certain conditions. Such as if a user fills out a form and forgets to enter their username. Instead of parsing, or executing the needs of that form, the script stops doing anything because you told it to exit. Exit can also hold a value. If exit or die hold a string, the browser will echo that string prior to stopping, like so:

<?php

exit('You forgot to enter your username');

// or

die('You forgot to enter your username');

?>

Hmm, see the 2 slashs I put there before the word "or"? That comments out the string after it. Meaning that if I want to leave comments in the source code that will never show in the browserand never be processed at all, I put those two slashes before that string. For each line that has a comment, you must add 2 new slashes before it or you can do this witha slash and asterix (star):

<?php

exit('You forgot to enter your username');

/*
	This is a comment
	that only someone editing this script
	will ever see. This part will NEVER show in the browser...
	Unless you forget the starting <?php tag on the first line
	of the script.

	Capisci?
*/

?>

Now before I close this article up, let's bounce back to variables for a moment. There are a few fules in creating variables.

  1. Unlike other languages, the variable is declared at the time it is defined. You do not have to declare a variable prior to assigning a value to it.
  2. A variable cannot be used if it is not declared. Doing so, will cause a message to show that will say something like Notice: Undefined variable.
  3. A variable MUST start with a dollar sign $.
  4. The first character after the dollar sign must be a single byte character (a through z) not any strange characters like Chinese or Persian.
  5. After those two first characters, the dollar sign and the letter, you can us letters, numbers and underscores _

To remove a variable, which rarely is needed, you use a PHP function called unset() like so:

<?php

$myName = 'Jeff';

echo $myName;

unset($jeff);

?>

To append one value to another value already contained inside a variable, you do not need to create antoher variable. Remember the period? The period can append values to eachother like so:

<?php

$myName = 'Jeff';
$myName .= ' Dilegge';

echo $myName;

// this will display Jeff Dilegge

?>

You can also append two different variables together witht he period, like so:

<?php

$myFirstName = 'Jeff';
$myLastName .= ' Dilegge';

echo $myFirstName.$myLastName;

// this will display Jeff Dilegge

?>

If you want a string between the two variables, you can also enter the single quotes like so:

<?php

$myFirstName = 'Jeff';
$myLastName .= 'Dilegge';

echo $myFirstName.' '.$myLastName;

// this will display Jeff Dilegge

?>

This concludes my very basic PHP tutorial for beginners. Next I will talk about passing variables from one script to another safely.


Links Directory | Jeffery Dilegge