PDA

View Full Version : In-depth Guide/Tutorial For PHP (Part One)



InternalExpertCoding
05-26-2011, 12:27 AM
Okay, So let's get started on just some basics.

Input: Any Data the computer collects from user's or device's
Output: Any Data the Computer,Program,Script Produces or Visually Shows to User's

ASCII "askee": Developed sometime in the early 1960's. The ASCII Character Set defines codes for 128 characters. (Will Post them all at the end of the segment).

Therefore, In the early 1990's, Something called Unicode was developed.

Unicode: Encoding scheme that is compatible with the ASCII Character set, But Unicode represents every character in any programming languages to this date. Unicode is now the "standard" character set used today.

PHP: A High Leveled Language:
There are many high-leveled languages out there. From, C,C++,And even Javascript.
Each high-leveled language has its own set o words a programmer must learn before using the language.
These are known as reserved words or key words.
Every key word, or reserved word has a specific meaning to it, And CANNOT, I repeat CANNOT be used as anything other than what it is defined for.

Some other things that high-leveled languages have are:
Operators
Syntax
Punctuation
Statements
And many others.

Operators perform various operations through data. Such as Algorithms.
Or, math operations. Such as ( 2*2 = 4)

Syntax, Now, This is a big one.
Syntax is a set of rules that are meant to be strictly followed when developing a script.
So, What does the Syntax rules do?
Well, Generally, They dictate how key words,operators, and punctuation characters should be used in a program.
Learning the Syntax of the language is the FIRST step in learning the language of your choice.


********UPDATE...Added in Starting and ending Syntax*****
As requested, I will go over the starting and ending syntax for PHP.
First off, There are 2 different ways that can be used when starting PHP code, Only one I recommend though.


<?
// Starting and ending Syntax. I don’t recommend.
?>


And,


<?php
// Starting and ending Syntax I do recommend.
?>

So, What’s the difference?
Well, Orginally, During PHP first years of development


<?
?>

Was used. Which was pretty simple and fast to write.
Although, In today’s world.


<?php
?>

Is used.
But why?
Well, <? ?> Makes your parser do a lot more work to consider it is PHP code.
The starting and ending syntax always tells your parser what type of code is going to be executed.
Therefore, <? ?> Or, <?php ?> Can be used.
Using the <?php ?> Method allows the parser to do less work to find out what type of code is going to be executed. Simply because, This method is the strict syntax in PHP now, As The Second method was Strict in earlier times of PHP.
So, during the tutorial, As I said, I will be using the recommended,


<?php

?>





Statements are simply instructions that are used when writing a script. This can consist of the following:
Key Words, Operators, Punctuation, and Other Allowable programming elements.
In which, They must be arranged in the correct sequence to perform the operation properly.
The statements that a programmer writes are called source code, or for simple, code.

So, per say you have written a statement. consisting of: Statements


<?php
show 'Hello World';
?>


This above code will give you a syntax error.
Which is simply a mistake in the source code, showing an error on the designated page.



<?php
echo 'Hello World';
?>

Would not give an error. As an "echo statement", Is 1 of two correct ways to properly display writing on a page.


<?php
print "Hello World";
?>

Would be the second correct way to execute and properly display writing on a page.

Take Notice at what I have changed here.

1. Changed echo to print
2. Changed Single quotes to Double Quotes

Now, Many expert programmer's would get into a fight about why to use echo over print, or vice-versa.
And, Might also do the same thing over single quotes vs. double quotes.
But, I am not.
Both are proper syntax rules to display content on a page.

So, overall, there are 4 proper ways to correctly display the content on a page.

1.


<?php
echo 'Hello World'; // Echo Statement Using Singles
?>


2.



<?php
echo "Hello World"; // Echo Statement Using Doubles
?>

3.


<?php
print "Hello World"; // Print Statement Using Doubles
?>

4.


<?php
print 'Hello World'; // Print Statement Using Singles
?>


Back to syntax and syntax errors.
As I was writing this up, I originally was going to write up syntax errors and post their meanings. But I found this: http://php.syntaxerrors.info/index.php?title=Call_to_undefined_function
So, As I said before, Syntax dictates how key words, operators, etc should be used in a script.
Therefore, If any syntax rules are broken, A Syntax Error will occur and display on the designated page showing 1 of the errors explained in that website.
But! Say, you are writing a script. Generally, Every script will contain at least 1 syntax error. Even if its coded by the best. Simply Mistakes such as spelling or a missing (;) Could trigger these errors. So, If you get any of those errors, don’t let it bring you down. Think of it as a incentive to do better.

Now, The code is up and running and in executable form. It is now tested by you for logical errors.
Logical Errors: Does not keep the program from running, But, It does keep it from producing correct results.
One of the most common logical errors are mathematical mistakes. Such as:


<?php
2*2 = 19;
?>

Clearly there you can tell there is a logical error.
As, I am not showing full codes it is quite difficult to explain. But I will show you a code that does have logical errors in it, But! The code will show things I have yet to go over.


<?php
$myVar1 = 2
$myVar2 = 3
$result = 7
$myVar1 * $myVar2 = $result
?>

Now as you can tell. 2 (*) 3 does not equal 7.
Therefore, processing a logical error.
So, You don’t want to keep it this way do you?
Nope, So the next step is to go back into the source code and debug it.
Debug: Simply means that you go back into the code, Find and correct the logical errors.


Input: Anything the Script takes action to.
Output: Displayed Content
Input is simply anything that the user does to provoke the script the execute.
Such as, When you log-in into a game, or website. And you must click “Log In”, Or “Submit”, You are sending 1. Your User Name , and 2. Your Password to the database.
In nearly all cases it will go somewhat like this:
1. User Enters Username
2. User Enters Password
3. User Submits Input (“Log In”)
4. Authenticates The Username and Password are Correct
5. Inserts some Content into the database
Output can contain anything from a website showing images, To it showing you something.
Before, I posted 4 methods that a Statement can be displayed. That in which, Would be considered output.
So, As I am doing this section over logging in to a game.


<?php
echo ‘You are Now Logged In, Redirection’;
?>

So, The steps above now change.
1. User Enters Username
2. User Enters Password
3. User Submits Input (“Log In”)
4. Authenticates The Username and Password are Correct
5. Inserts some Content into the database
6. Output is now inserted “ You are Now Logged in, Redirection”
The output will be showed on the page, allowing you to read that statement.
Please, Note: The Quotes (either single or doubles) Are not displayed. They are simply mark the start of the output, and the end of the output. Therefore, being fully closed by a semi-colon ( ;)
As I forgot to mention earlier, While writing a source code, The code executes from top to bottom. Which is very important to know. So, A set of statements that are executed from “TOP TO BOTTOM” are called sequence structures.
Hence the name (Sequence), It will simply tell you that there is some order there.




This Concludes the First Part. As I don't have time to write the rest of it up yet. Back with more later.
Please Comment :)

Quick Update :)

Djkanna
05-26-2011, 04:10 PM
Good starting guide, well done! :)

InternalExpertCoding
05-26-2011, 04:12 PM
Thanks DJK, More to come soon. :)

InternalExpertCoding
05-26-2011, 09:37 PM
____Update ____

Strings & String Literals. Say What?

String: A sequence of characters that is used to collect data.
String Literal: String that appears in the actual source code.
Therefore,


<?php
Echo ‘Hello World’;
?>

Hello World, Is a string literal.
Most of all string literals are enclosed by single or double quotes. As I mentioned there is not much of a difference there earlier.

Variables ($myVariable)

So, Variables, What do they do and why?
Well, Variables are made to store specfic data in a code therefore, you can perform some type of operation on it.
So, a variable is simply a storage location in memory, and is represented by a name.
For example:


<?php
$myVar1 = 2;
$myVar2 = 3;
$result = 7;
$myVar1 * $myVar2 = $result
?>


$myVar1 is simply storing the numerical value “2”.
$myVar2 is storing the numerical value “3”.
And $result is storing the numerical value “7”.
Therefore, I can process an operation like:


$myVar1 * myVar2 = $result

Using just variables. In which, later on, if I want to change the script to execute a different mathematical function, I would only have to change my variables. Which is an easy reference.
Now, Let’s take a look at the way I have named my variables.


$myVar1
$myVar2
$result

Now, In any language, The Syntax allows you to name your variables to whatever is suitable for what action your processing.
But, there are some restrictions on naming variables also. Like, Variable names cannot contain spaces.
Most programmers use a method called camelCase to make the variable name more readable.
As you see about, I have used camelCase.
Example:


$myVar1

Which, I will be using this method through my tutorial, As it makes reading the variable name much easier.
Another way to easily read the variable name while using multiple words is to separate them using a underscore.
Example:


$my_Var

So, Either of those two ways are ones I perfer to use when naming a variable.
Now, You have been looking at quite a few variable names here.
I guess you are wondering what the “$” stands for?
The Dollar sign indicates that this is a storage variable.
Which, In PHP, you must always use the $ to declare the variable.


__UPDATE, Finishing of variables__

Continued: Variables and Variable Assignments

As you seen before,


$myVar1 = 2;
$myVar2 = 3;
$result = 7;

Was used. Setting each variable with a specific numerical value.
This method is called assignment statements.
For example:


<?php
$myVar1 = 25;
echo ‘I have ‘ .$myVar1. ’ dollars in my bank account’;
?>
// This code used as your input
//For input (Refer back to the last section)
?>

So, your output on the page would be.
I have 25 dollars in my bank account

DreamCoder
05-27-2011, 07:10 PM
Well done IEC cant wait for more!!

Kieran-R
05-27-2011, 07:39 PM
Great starting guide. But can I suggest you post this in your blog? It would be a lot easier to follow and access :)

InternalExpertCoding
05-27-2011, 07:50 PM
I don't come here much at all. So didn't know about the blogs. But, Ill try to :)

**NOW POSTED TO MY BLOG**

I think i might still post it in both spots? Not sure though

galdikas
05-27-2011, 11:56 PM
I don't come here much at all. So didn't know about the blogs. But, Ill try to :)

**NOW POSTED TO MY BLOG**

I think i might still post it in both spots? Not sure though

Yeah I would say it should be ok. The more spots it is posted in the easier it will be for people in need to find this brilliant tutorial ;)

Danny696
05-28-2011, 09:32 AM
Its good, but if its an In-depth guide, you need to start from the very top, which you havent. By this I mean basic PHP syntax, such as <?php etc.....

InternalExpertCoding
06-01-2011, 02:12 AM
Its good, but if its an In-depth guide, you need to start from the very top, which you havent. By this I mean basic PHP syntax, such as <?php etc.....


I guess I just over-looked that really. "Simple" Starting Syntax should have been in the tutorial. But, Common Sense. :P
Anyways, Ill go back through and place that in there. :)

Thanks for the comments you guys, Glad to get some feedback on it :)

Dominion
06-01-2011, 01:02 PM
I guess I just over-looked that really. "Simple" Starting Syntax should have been in the tutorial. But, Common Sense. :P
Anyways, Ill go back through and place that in there. :)

Thanks for the comments you guys, Glad to get some feedback on it :)
Good tutorials ignore “Common Sense” ;)

Danny696
06-01-2011, 01:11 PM
Good tutorials do ignore common sense, but this is an In-depth guide, of the basics, which would include basic php syntax, what if someones confused about started a php file, or what <?= means? Then we get some n00by post dont we?

a_bertrand
06-01-2011, 01:42 PM
Danny we got your point, now if you are better are writing tutorials then produce some instead of complaining about the one someone else did.

InternalExpertCoding
06-01-2011, 07:42 PM
Do you want me to re-name it "Almost" In-Depth Guide?

Seriously. I added the syntax in there, But I can't be assed to add every single detail in this guide. It gets the main point across, And has more detail than most of others Iv seen around.

The tutorial comes from my teaching strategies which as I have said before, I use to teach PHP. So Usually, The first thing I go over while I teach the echo and print statements is the starting and ending syntax. But just by looking at the code above, It is started and ended with tags, Therefore, Common Sense....

DFTskillz
06-19-2011, 11:33 AM
Omg this is great! :O