Jump to content
MakeWebGames

PHP - Just Getting Started


InternalExpertCoding

Recommended Posts

This is an old Tutorial I made that I think could be useful to a newly found coder.

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.

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


[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
show 'Hello World';
?>

[/TD]
[/TR]
[/TABLE]



 

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.

 



[TABLE]
[TR]
[TD="class: gutter"]1

2
3
4
[/TD]
[TD="class: code"]<?php
echo 'Hello World';
?>

[/TD]
[/TR]
[/TABLE]



 

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



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
print "Hello World";
?>

[/TD]
[/TR]
[/TABLE]



 

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.

 


[url="http://makewebgames.io/#"]?[/url]
[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
echo 'Hello World'; // Echo Statement Using Singles 
?>

[/TD]
[/TR]
[/TABLE]



 

2.


[url="http://makewebgames.io/#"]?[/url]
[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
echo "Hello World"; // Echo Statement Using Doubles 
?>

[/TD]
[/TR]
[/TABLE]



 

3.



[TABLE]
[TR]
[TD="class: gutter"]1
2
3


4

[/TD]
[TD="class: code"]<?php
print "Hello World"; // Print Statement Using Doubles
?>


[/TD]
[/TR]
[/TABLE]



 

4.



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
print 'Hello World'; // Print Statement Using Singles 
?>

[/TD]
[/TR]
[/TABLE]



 

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.p...fined_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:



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
2*2 = 19;
?>

[/TD]
[/TR]
[/TABLE]



 

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.



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
5
6
7
[/TD]
[TD="class: code"]<?php
$myVar1 = 2
$myVar2 = 3
$result = 7
$myVar1 * $myVar2 = $result
?>

[/TD]
[/TR]
[/TABLE]



 

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.



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
echo ‘You are Now Logged In, Redirection’;
?>

[/TD]
[/TR]
[/TABLE]



 

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.

____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,



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]<?php
Echo ‘Hello World’;
?>

[/TD]
[/TR]
[/TABLE]



 

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:



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
5
6
7
8
[/TD]
[TD="class: code"]<?php
$myVar1 = 2;
$myVar2 = 3;
$result = 7;
$myVar1 * $myVar2 = $result
?>

[/TD]
[/TR]
[/TABLE]



 

$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:



[TABLE]
[TR]
[TD="class: gutter"]1
2
[/TD]
[TD="class: code"]$myVar1 * myVar2 = $result

[/TD]
[/TR]
[/TABLE]



 

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.



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]$myVar1
$myVar2
$result

[/TD]
[/TR]
[/TABLE]



 

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:



[TABLE]
[TR]
[TD="class: gutter"]1
2
[/TD]
[TD="class: code"]$myVar1

[/TD]
[/TR]
[/TABLE]



 

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:



[TABLE]
[TR]
[TD="class: gutter"]1
2

[/TD]
[TD="class: code"]$my_Var

[/TD]
[/TR]
[/TABLE]



 

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,



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
[/TD]
[TD="class: code"]$myVar1 = 2;
$myVar2 = 3;
$result = 7;

[/TD]
[/TR]
[/TABLE]



 

Was used. Setting each variable with a specific numerical value.

This method is called assignment statements.

For example:



[TABLE]
[TR]
[TD="class: gutter"]1
2
3
4
5
6
7
8
[/TD]
[TD="class: code"]<?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)
?>

[/TD]
[/TR]
[/TABLE]



 

So, your output on the page would be.

I have 25 dollars in my bank account

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...