You can now donate to EvilGamerZ, HERE!

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Vox

Pages: [1]
1
Coding & Programming / Basic PHP (Lesson 4)
« on: 12 Mar 2010 - 04:11 »
-Note, it really helps to have at least a basic knowledge of HTML as well as JavaScript before working with PHP, although its necessarily required, you are going to need to learn it eventually.

Hot damn! it's been a long time since the last tutorial hehe, sorry for the wait!

---------------------------------

Lesson-4:What is an array?
Well arrays are very unique and wonderful things in the world of PHP. So what is an Array exactly? Well for starters, Arrays are Very Similar to Variables. As we know, variables are used as a storage area holding a number or text. However, a Variable only holds one value, and if your working with a great amount of items, variables aren't the way to go. This is where arrays come in. Unlike Variables, Arrays store multiple values in one single variable. You can almost refer to an array as a 'special variable' because it is far more advanced than a regular variable.

Arrays are good for a lot of things in PHP, in fact there good for almost every situation where you need to create and use multiple variables. Lets say you have a list of chips,storing the chip types in single variables might look like this:

Code: [Select]
<?php
$chips1
="Fritos";
$chips2="Doritos";
$chips3="Cheetos"
?>

Although this way works, what if you want to loop through a whole bunch of chip types and find a specific one? What if you had a list of over 200 brands of chips! The best solution in this situation would be to use an array.

With an array,you can hold all your variable values under a single name. You can also access the values by referring to the array name, instead of having to create a whole new variable. Each little element within the array, has its own index which makes it way easier to be accessed.

Although there are only 3 types of arrays in PHP, all of them are useful.
The 3 types include:

Numeric array - An array with a numeric index.
Associative array - An array where each ID key is associated with a value.
Multidimensional array - An array containing one or more arrays.
Lets go over them shall we?

Lesson-4:Numeric Arrays.
First lets begin with what an "Numeric Array" actually is. A Numeric Array stores each array element with a numeric index (I'll explain). Now since,there are only two methods to create a numeric array, its most smart to start off with the worse way to do it, although they both work fine.

METHOD 1
Code: [Select]
<?php
$chips
=array("Fritos","Doritos","Cheetos","Ruffles");
?>

How ever, that does absolutely nothing lol. So let's go ahead and make it do an action by adding an echo to the script.
Code: [Select]
<?php
$chips
=array("Fritos","Doritos","Cheetos","Ruffles");
echo 
"The Third bag of chips is ".$chips[2];
?>

The output of this would be:
Code: [Select]
The Third bag of chips is Cheetos stats
Explanation : METHOD 1

First of all, lets explain this:
Code: [Select]
$chips=array("Fritos","Doritos","Cheetos","Ruffles");This is a simple line of code, which sets all your variables within the array "$chips". Any of these can be accessed through a 'Numeric Code' which will be explained next.

Code: [Select]
echo "The third bag of chips is ".$chips[2];This is a simple command. First we have the "echo" command which is the basic component for making our php script show our beautiful text. Since we used an array, (instead of something like "$chips2,$chips3," etc.) Because of that, we simply type the name of the array".$chips" and yes the 'dot' is important. Now the important part of this is the "[2]" the 2 represents the third variable within our array. The thrid variable happens to be "Cheetos".

------------------Now your probably wondering
     Q:"How the F*** is 2 going to be Cheetos when Doritos are clearly the second word?"
        A:Well like i stated before the index starts at "0" so instead of "Fritos" being "1" the number  is "0".


METHOD 2
Code: [Select]
<?php
$chips
[0]="Fritos";
$chips[1]="Doritos";
$chips[2]="Cheetos";
$chips[3]="Ruffles";
echo 
$chips[2] . " and " $chips[3] . " are really delicious.";
?>

The output of this would be:

Code: [Select]
Cheetos and Ruffle are really delicious.
Explanation : METHOD 2

First of all, lets explain this:
Code: [Select]
$chips[0]="Fritos";
$chips[1]="Doritos";
$chips[2]="Cheetos";
$chips[3]="Ruffles";
This is alot like the other method, only in this one,we are manually assigning the numeric index to the variable.Both ways are effective but this one prevails for one big reason, that reason being the way easier ability to figure out the index number.

Code: [Select]
echo $chips[2] . " and " . $chips[3] . " are really delicious.";This is a simple command. First we have the "echo" command which is the basic component for making our php script show our beautiful text. Much like the first version, we use "[NUMBER]" for defining a variable value. The text after the second to last quote, is the text to be displayed after the values are displayed.

Lesson-4:Associative Arrays.
In an associative array, each ID key is associated with a value, which is kind of crazy because now you have an array,variable,index id, and a value to go along with all of it, sure gets confusing sometimes. Non the less, we can get through it :D

Basically when storing data about specific named values, such as figuring out who bought which bag of chips,a numerical array is not always the best way to do it. So thankfully,with associative arrays we can just use the values as keys and assign values to them (crazy hehe!) and like Numeric Arrays they both have 2 methods that go along with them (oh happy joy >.> lol)

METHOD 1
Code: [Select]
<?php
$chips 
= array("Dane"=>Fritos"Ray"=>Cheetos"Lorenzo"=>Doritos"Tyler"=>Ruffles); 
?>

Now since this way isn't really all the cool and important (lul) i'll go over it quickly.

Explanation : METHOD 1
[/color]
Code: [Select]
$chips = array("Dane"=>Fritos, "Ray"=>Cheetos, "Lorenzo"=>Doritos, "Tyler"=>Ruffles);The things that make this code different than the numeric array are the way its set up. Before we used "Tyler" and that would be all we needed.But now were working with Associative Arrays so we need to add =>value, which COULD look like this:
Code: [Select]
"PHP"=>Fun
METHOD 2
Code: [Select]
<?php
$chips
['Dane'] = "Fritos";
$chips['Ray'] = "Cheetos";
$chips['Lorenzo'] = "Doritos";
$chips['Tyler'] = "Ruffles";

echo 
"Ray purchased" $chips['Ray'] . " from albertsons.";
?>
Output of this would be:
Code: [Select]
Ray purchased Cheetos from albertsons.
Explanation : METHOD 2
Nothing really to explain here. I took Method 1, and just created it differently, instead i just manually added the index name instead of writing all of them on a single line.


Lesson-4:Multidimensional Arrays.
Ok were down to the final type of array, the Multidimensional array.Basically in a multidimensional array, each element in the main array can also be an array, (like an array within an array make sense?). and each element in the sub-array can be an array, and it can go on and on and on and on etc.

Example time!:

Code: [Select]
<?php
$Drinks 
= array
  (
  
"Coke"=>array
  (
  
"Sprite",
  
"Dr.Pepper",
  
"Root Beer."
  
),
  
"Tea"=>array
  (
  
"Arizona Ice Tea"
  "Green Tea"
  
),
  
"Other"=>array
  (
  
"Powerade",
  
"Water",
  
"Apple Juice"
  
)
  );
?>

Let me explain this through comments so its easier to understand and compare.

Code: [Select]
<?php //start of your script
$Drinks = array //Beginning of Array
  
(
  
"Soda"=>array //Main-Array
  
(
  
"Coke"//Sub-Array
  
"Dr.Pepper"//Sub-Array
  
"Root Beer." //Sub-Array
  
), //acts as a divider.
  
"Tea"=>array //Main-Array
  
(
  
"Arizona Ice Tea" //Sub-Array
  
"Green Tea" //Sub-Array
  
), //acts as a divider.
  
"Other"=>array //Main-Array
  
(
  
"Powerade"//Sub-Array
  
"Water"//Sub-Array
  
"Apple Juice" //Sub-Array
  
)
  ); 
//Ends the list.
?>
//end code

Now that you are familiar with this method, lets make it do something!

Code: [Select]
<?php
$Drinks 
= array
  (
  
"Soda"=>array
  (
  
"Coke",
  
"Dr.Pepper",
  
"Root Beer."
  
),
  
"Tea"=>array
  (
  
"Arizona Ice Tea"
  "Green Tea"
  
),
  
"Other"=>array
  (
  
"Powerade",
  
"Water",
  
"Apple Juice"
  
)
  );

echo 
"Is " $Drinks['Soda][2] .
" a type of soda?";
?>


Explanation of the newly added code through comments.

Code: [Select]
<?php
$Drinks 
= array
  (
  
"Soda"=>array
  (
  
"Coke",
  
"Dr.Pepper",
  
"Root Beer."
  
),
  
"Tea"=>array
  (
  
"Arizona Ice Tea"
  "Green Tea"
  
),
  
"Other"=>array
  (
  
"Powerade",
  
"Water",
  
"Apple Juice"
  
)
  );

echo 
"Is " $Drinks['Soda][2] . //Go's through the first variable Drinks,Then to Soda,and then 2kind of like a chain of events.
" a type of soda?"
?>


The "Is" and " a type of soda?" were both the beginning and end for what text we wanted displayed on each side of the value.

THUS THE OUTPUT WOULD BE!!! DUN DUN DUN!!!

Code: [Select]
Is Root Beer a type of soda?
Thus to yourself you can successfully say, "hell yeah its a soda and i know more php!" That's right yell it loud and proud!

*Not responsible if parents smack you for yelling :D*


END OF LESSON-4

Summary
In this tutorial we learned all the different types of arrays and how to use them. I really didn't expect the tutorial to come out with this amount of size, so instead of stressing everyone out, im just going to write another tutorial for Lesson 5 getting back on schedule with what i promised you guys.

Next lesson
While Loops and For Loops -I promise.

Contact
MSN - voxkilla@hotmail.com
Email - synysterray@gmail.com
Here on the forums.
Text. - friends and private users only.

Total Tutorial Time: 3 hours. Grrr


[Edited by Razor: Stickied]

2
Coding & Programming / Basic PHP (Lesson 3)
« on: 22 Dec 2009 - 11:54 »
-Note, it really helps to have at least a basic knowledge of HTML as well as JavaScript before working with PHP, although its necessarily required, you are going to need to learn it eventually.

I am also known adding php comments next to the codes for a simpler evaluation of the codes as well as making things less complicated so make sure you notice the // next to pieces of code.

---------------------------------

Lesson-3:What is an If...Else Statement?
More preferably known as Conditional Statements, are used to perform different actions based on different conditions. Basically the true and false of the php world. Most easily used in forums and websites when checking to see if someone needs to register or continue to search the website as they have already submitted their information.

Lesson-3:If Statement.
An if statement is only used to execute some code only if a specified condition is true, which means you cant have the date being Monday and having the echo command say "Happy Saturday!". It just simply wont fit for this specific statement. This means you can only use the If Statement, if the condition only has a true answer.

Example:

Code: [Select]
<html>
<body>

<?php //beginning of the php script.
$d=date("D"); //this is our variable standing for the date.
if ($d=="Tue") echo "Be Sure to play EvilGunZ All Day! its Tuesday!"//this is the if statement, it&#39;s stating that if today is Tuesday to show the echo output.
?>
//end of the php script

</body>
</html>

Now with that said, you'll notice for this syntax, you won't see the else statement, this is because like i stated before, that if statements only have one true answer, if they are false, it will appear blank and will continue to be blank until its true again.

Lesson-3:If Else Statements.

Although If statements are good for simple information needing to be displayed, it's just not good enough for incorporating more complex syntax into the script. If you need more than one statement to appear whither the condition is true or false you use If Else Statements.

Example:

Code: [Select]
<html>
<body>

<?php
$d
=date("D"); // our variable giving the value of d to be date.
if ($d=="Tue"//this is the true statement, shown if the statement is true.
  
echo "thank god Monday is over";
else 
//this is preparing the browser for the false statement if the above statement comes out false.
  
echo "today is not Tuesday....go buy a calendar my lil php eyes hurt T_T"//this will instead be displayed, if it is not Tuesday
?>


</body>
</html>

Since today is Tuesday (according to me) it should come out saying "thank god Monday is over". Yes, this works for any day of the week (Sun,Sat,Mon,Tue,Wen,Thu,Fri.)
--------------------------------
---------------------------
-----------------
This also works with proving numbers.
Example of a true and false number statement:

True:

Code: [Select]
<html>
<body>
<?php
$number_leet 
1337//This is are variable

if ( $number_leet == 1337 ) { //This is asking as a statement if the variable is equal to 1337
echo "The Number is in fact, leet"//if its true this will show up
} else {
echo "Retard, you can&#39;t do math!"//if not
}
?>


</body>
</html>

But i want to make this wrong to give you a better idea, and to do that, we do this:

Code: [Select]
<html>
<body>
<?php
$number_leet 
6666;

if ( 
$number_leet== 1337 ) {
echo "The Number is in fact, leet"//if true
} else {
echo "Retard, you can&#39;t do math!";//if false
}
?>

</body>
</html>

In which it will come out as us being retarded :).


Lesson-3:ElseIf Statements.
If the "If Else Statements" just didn't tickle your fancy enough, and you want even MORE true statements and options for your script, then i think you'll be happy to know, IM GONNA TEACH YOU! yaaaaaaay.

An Else If statements, is used for a series of true statements, like if you wanted to display every day of a school week, and how dreadful it is, and on the weekends wish everyone a happy day! then this is how it would be done.

Example:

Code: [Select]
<html>
<body>

<?php
$d
=date("D"); //variable for the date
if ($d=="Mon"//first true statement
  
echo "School is lame! good luck :(!";
elseif (
$d=="Tue"//second true statement
  
echo "School is lame! good luck :(!";
elseif (
$d=="Wen"//Third true statement
  
echo "School is lame! good luck :(!";
elseif (
$d=="Thu"//Fourth true statement
  
echo "School is lame! good luck :(!";
elseif (
$d=="Fri"//Fifth true statement
  
echo "School is lame! good luck :(!";
else
  echo 
"Have a BEAUTIFUL DAY SCHOOLS OUT!!";
?>


</body>
</html>

Now, your probably wondering, "wait a minute, if Monday - Friday are true statements, and Saturday and Sunday are false statements, then how could today be Saturday and still be true?" Well just because a statement is true, that DOESN'T mean it is true. Though the statement itself is meant to be true, that doesn't mean that it has to show up.

Not all true statements in an IfElse/ElseIf statements come up. "Why?" Because they are true from Monday - Friday. If you check the script any day of those 5 selected one of the following true choices will come up, but if its not any day between Monday and Friday this means the true statements are false and that's when the "else" statements come into action giving the output "Have a BEAUTIFUL DAY SCHOOLS OUT!!" Because you are on another day of the week besides Monday - Friday.

Lesson-3:Try it out-Statements!.
-Yes, i decided to add a new little sub section to give the basic format or the jist of what the section was about for people to use. I suggest using the knowledge from previous tutorials and this guide for information.

Try it out, since today is Tuesday, use this format and edit in your own things from what you learned above:

Basic Format:
Code: [Select]
<html>
<body>

<?php
$d
=date("D");
if (
$d=="THE DAY")
  echo 
"TEXT TO BE DISPLAYED IN A TRUE STATEMENT";
elseif (
$d=="THE SECOND DAY"
  echo 
"TEXT TO BE DISPLAYED ON ANOTHER TRUE STATEMENT";
else
  echo 
"TEXT TO BE DISPLAYED ON A FALSE STATEMENT";
?>


</body>
</html>

http://evilphp.site11.com/days.php

Lesson-3:Switch Statements.
Switch statements can be really confusing if learned wrong or not knowing what to do. I understand them really well and i can explain them to you very easily :).

Well a switch statement is usually used like a search engine, containing a variable,some echo statements,and break lines. We can use switch statements for a lot of things, (BTW this is my favorite subject so I'm really happy right now in 3 am in the morning rofl) Questions,information and for searching for what we need exactly without having to scroll for it.

For Example, say we have a php script full of our favorite website addresses and we seem to have forgotten how to get to EvilGamerZ. Now i don't have time to write a lot of website addresses and since this is an example, go along with it, but in real time, these statements can save your butt a lot of searching. Using the Switch Statement we can find how to get to EvilGamerZ.

Example:

Code: [Select]

<html>
<body>

<?php
$sitename 
"EvilGamerZ"// variable
echo "How do i get to $sitename ? <br />"
switch (
$sitename){
case "EvilGamerZ":
echo "Go to evilgamerz.net";
break;
case "Youtube":
echo "Go to youtube.com[/url]";
break;
case "Google":
echo "Go to google.com[/url]";
break;
}
?>

</body>
</html>

The output:

Code: [Select]
How do i get to EvilGamerZ ?
Go to evilgamerz.net

Ok now for the explaining of all this. What we did was state a variable giving it the name "$sitename" with the value of where we want to go that being "EvilGamerZ.

Code: [Select]
echo "How do i get to $sitename ? <br />";
switch ($sitename){

Okay, those two lines are VERY important. let me separate them and break them down for less confusion.

"echo "How do i get to $sitename ? br />";" - <> must be around "br/"

Now what we have here is basically the output for our first line, which contains the variable of the value we are looking for. Now it is very important that you have the variable you are searching for inside of the are of text, as well as the break line. First, without the variable, php has no value to search for and you will for sure get an error. Now the break line is especially important because it stops that line from interfering with the other syntax.

"switch ($sitename){" switch is the statement text that obviously must be there for any conditions to be ran through. That is the correct format for searching for a variable value as well as whatever comes with it. That piece of code is telling php to run through all cases looking for a match equivalent to that of the variable value given, that is the first step it uses to find it.

Code: [Select]
case "EvilGamerZ":
echo "Go to evilgamerz.net[/url]";
break;

lets break that down shall we?

"case "EvilGamerZ":" that is our case, or shall i say our variable value we are looking for by making "$sitename" equal "EvilGamerZ" it gave that case a clear lead  in the search by having every character match up.

"echo "Go to evilgamerz.net";" This is what is to be displayed once your value is found, this and only this will load on your browser because it is the only echo line under you value.
"break;" this is super duper important, the reason why is look:

Code: [Select]
case "EvilGamerZ":
echo "Go to evilgamerz.net";
break;
case "Youtube":
echo "Go to youtube.com";
break;
case "Google":
echo "Go to google.com";
break;

if you notice, they all have break at the end of them. This is to ensure that once the correct value is found, nothing below it will be added as well, without the break, everything below EvilGamerZ, would show up as well and that is a big no-no.
--------------------------------
-----------------------------------------
--------------------------------
Now sometimes and sadly,  you may not find what your looking for, so for this we add another lil bundle of code at the bottom to ensure your not totally let down.

Example:
Code: [Select]

<html>
<body>

<?php
$sitename 
"EvilGamerZ"// variable
echo "How do i get to $sitename ? <br />"
switch (
$sitename){
case "Ask":
echo "Go to ask.com";
break;
case "Youtube":
echo "Go to youtube.com";
break;
case "Google":
echo "Go to google.com";
break;
default:
echo "Sorry, we couldn&#39;t find it! <3";
break;

}
?>

</body>
</html>

Since the variable value for EvilGamerZ was not their, the default echo is "Sorry, we couldn't find it! <3".
 
The reason it goes to default is simple, If is to else what switch is to default, If the "if statement" is false the "else clause" backs it up, if the "switch statement" returns empty, the "default case" will ensure it hides the mistake.

That's how switch statements are used in php. They are good for a lot of things, and they are a way more efficient way to program, plus its a speedier way to check things then having to write "ElseIf" a billion times, and sometimes a statement just doesn't cut it for being the best way to check for specific conditions.

Lesson-3:Try it out-Switch Statements!.
Try it out, use this format and edit in your own things from what you learned above:

Code: [Select]
<html>
<body>

<?php
$variablename 
"VARIABLEVALUE"// variable name and value
echo "QUESTION/ASK/ETC $sitename ? <br />"
switch (
$variablename){
case "NAME":
echo "INFORMATION/ANSWER";
break;
case "NAME":
echo "INFORMATION/ANSWER";
break;
case "NAME":
echo "INFORMATION/ANSWER";
break;
default:
echo "DEFAULT/TEXT"//if there is no true statement.
break;

}
?>

</body>
</html>

END OF LESSON-3

Summary
In this tutorial we learned what switch and if/else/elseif  statements were and how to use them in a useful and helpful matter, i can only hope that you benefited from this, and hope you aren't afraid to ask questions. I hope as we progress through these tutorials you are starting to learn more and more on how php works and how to use it. We're almost on the advanced level, so keep up the good work!

Next lesson
While Loops and For Loops

Contact
MSN - voxkilla@hotmail.com
Email - fakdapr0z@gmail.com
Here on the forums.
Text. - friends and private users only.

Total Tutorial Time: 2 hours. Cant beleive i did this so late lol.



[Edited by Razor: Stickied]

3
Coding & Programming / Basic PHP (Lesson 2)
« on: 21 Dec 2009 - 04:44 »
-Note, it really helps to have at least a basic knowledge of HTML as well as JavaScript before working with PHP, although its necessarily required, you are going to need to learn it eventually.

---------------------------------

Pre Note-
In this tutorial, i am going to explain to you what variables and strings are and how they work, along with examples. I suggest if you haven't already to read Basic PHP (Lesson 1) or else all of this probably wont make sense to you.

Lesson-2:What are variables?
Well there actually not all that complicated and are useful in a lot of circumstances. Basically variables exist in PHP to be used for storing values (arrays,numbers,etc.).With that,every time one of your variables is called, it can be used over and over again in your script, meaning you can use it within your entire script more than once.It's very important to know that all PHP Variables start with a $ sign symbol, without this symbol it will not work.

Lesson-2:Testing a variable.
Well, to star it off the format for the correct way to declare a variable is

Code: [Select]
$var_name = value;
"$" Must be in front of a variable or it won't work.

"var_name" (Variable Name) Something like clr,txt,hi,etc.

"=" an equal sign put after the variable name to show what its equality is.

"Value" Value of the variable.

";" ends the code line.

---------------------------

Here is my interpretation of how to write my name without having to write it from an echo.

Code: [Select]
<?php
$a
"RainhasName";
$RainhasName"Ray";
echo ${
$a};
?>


In that script, i create three variables, let me break it down for you, then i'll explain how this would show up on a web browser.

"$a= "RainhasName";" This was the first variable we made, classifying "a" as equaling the value "RainhasName" using the "$" sign.

"$RainhasName= "Ray";" We now used the variable we created in the second line to provide another value statement.

"echo ${$a};" Now, as you have learned before echo sends out what ever is put next to it (provided its in the correct order) out to web browser to be seen by you/others.

"${$a};" Ok, this might sound confusing but here we go. That piece of code right their, starts from $a and ends on the variable "ray". Since "$a = "RainhasName"" and

"$RainhasName = "Ray";" which from that we added "echo ${$a};" which is like saying, echo "Ray"; but instead of having to type it all the time, we set a variable and as long as we have that variable we can use it in a lot of different situations.

So, basically by using this method of variables, your passing a string from one page to another, eventually leading up to using that variable of the same name, to get information.

Lesson-2:Variable Rules.

Cyan = quote from site
Green = from me and my knowledge

-A variable name must start with a letter or an underscore "_" (courtesy of w3)
-A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) (courtesy of w3)

-A Variable name cant contain any spaces, if you need to space it, use an underscore (_).
-Be very case sensitive when trying to pull information from a string ($a) and ($A) are not the same as far as PHP is concerned.
-The variable name can be anything, their is no specific list for anything you can use, however you should try to remember what that variable name is.


Lesson-2:What are strings?
String variables are used to store and manipulate text and values that contain characters. In another way, after we create or string, we can do other things to it or manipulate it.

After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable. It's basically close to the same thing as a variable as they are both used with eachother almost always.

Example:

Code: [Select]
<?php
$number
="Five Hundred Twelve";
echo 
$number;
?>

Basically what i did, was give a Variable with the name Number the value of "Five Hundred Twelve" and write echo to preform that variable into an output for you to see. In which it would show up on the web browser as "Five Hundred Twelve".

There are other ways to manipulate your string such as different functions and operators.

Lesson-2:Manipulating Strings:Operators

Another way of manipulating strings is The Concatenation Operator. It is used to put two string values together, and it is the only operator in PHP. The Concatenation Operators symbols is (.) simple isn't it lol?

Example:

Code: [Select]
<?php
$txt1
="This is the first sentence hehehe!";
$txt2="This is the second sentence! bet you didn&#39;t see that coming!";
echo 
$txt1 " " $txt2;
?>

By using that format, the operator is taking those two strings and merging them into one line without actually doing it, this leaves it both easier to edit and easier to see, its also simple. Also, if you notice the third string of the script, the operator was used two times, this was done to add a space to separate the two strings so it would look better then being scrunched up.

If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string (a space character), to separate the two strings.

Now that were done explaining the only operator in the PHP world lets get into some functions.

Lesson-2:Manipulating Strings:Functions

Now were going to get into how to find the length of a string. That step is called the strlen() function (or string length).

Example:

Code: [Select]
<?php
echo strlen("Guess how many characters are in this sentence.");
?>

Now if you had counted them all, ( which i hope you didn't as you should be testing all of this by uploading it to your FTP server and checking it in the web browser) you would know there is 47 characters. BUT, why would you want to do all that when you can simply at the strlen next to the output text.

Without:

Code: [Select]
<?php
echo "Guess how many characters are in this sentence.";
?>

"Guess how many characters are in this sentence."

With:

Code: [Select]
<?php
echo strlen("Guess how many characters are in this sentence.");
?>

"47"

The length of a string is often used in loops or other functions, as it is important to know when the string ends. because if it was a loop, we would want to stop the loop after the last character in the string. See the difference?

------------------------------------------------------
------------------------------------------------------
Now if were trying to find a specific character within a string we use a function called the strpos() function ( or string position)

Example:

Code: [Select]
<?php
$animalstring 
"Dog"

$oPos strpos($animalstring"o");
echo 
"The position of o in our string was $oPos";

?>


The out put would be "The position of o in our string was 1 " instead of 2 because php starts counting from 0. However if no match is found, the result will come out false.

Although, sometimes you may have more than one character of the same string in that script, in that case you'll want to do this.

Example:

Code: [Select]
<?php
$animalstring
"DogDogDogDogDogDogDog";

$oPos strpos($animalstring"o");
echo 
"The position of o in our string was $oPos";
$oPos2 strpos($animalstring"0"$oPos 1);
echo 
"<br />The position of the second o was $oPos2";
?>


In which the output would be
Code: [Select]
The position of o in our string was 1
The position of the second o was 4

However, we might need to find more than just 2 o's to do that, we add a little bit more text.

Code: [Select]
<?php
$animalstring 
"DogDogDogDogDogDogDog";
$offset 0;
$oCounter 0;

if(
strpos($animalstring"o") == 0){
$oCounter++;
echo "<br />o #$oCounter is at position - 0";
}

while(
$offset strpos($animalstring"o"$offset 1)){
$oCounter++;
echo "<br />o #$oCounter is at position - $offset";
}
?>

The output to this would be:

Code: [Select]
o #1 is at position - 1
o #2 is at position - 4
o #3 is at position - 7
o #4 is at position - 10
o #5 is at position - 13
o #6 is at position - 16
o #7 is at position - 19

I know it looks pretty confusing but don't fret, ill break the important parts down for you.

Code: [Select]
if(strpos($animalstring, "o") == 0){
$oCounter++;
echo "<br />o #$oCounter is at position - 0";
}

This is the first part of the script, this is going to check if their is a(n) o at position 0 first.

Code: [Select]
while($offset = strpos($animalstring, "o", $offset + 1)){
$oCounter++;
echo "<br />o #$oCounter is at position - $offset";
}

Even though in our script we did not find an o on position 0, there are still alot of other o's we need to find through out the script, and that bit of text solves it.

"$offset = strpos($animalstring, "o", $offset + 1)" This is referred to as a while loop, which is something used to accomplish large tasks with ease (i'll get into it in further tutorials). If this turns out to be false the loop will stop.

"strpos($animalstring, "o", $offset + 1)" This is almost like searching for a string position normally, only this time we added "$offset + 1" since the original variable was "$offset = 0" it means we start at the beginning and by adding "$offset + 1" it is going to skip each individual one looking for a match.

"$oCounter" This is almost like "$Offset = 0" in the fact that it starts us off at 0. When you have "$oCounter++" ++ is called an increment and since our "$oCounter = 0" and if you do the math with "$oCounter++"  it's basically just adding another string to its searching process with every missed attempt.
-------------------------------
-------------------------------
END OF LESSON-2

Summary

YAY! In this tutorial i showed you what strings and variables were, how to use variables in a script as well as delivering it from a variable to variable. I also showed you how to find a strings length and position which is very useful information for looping.

Next lesson
If...else and Switch Statements in PHP.


Total Tutorial Time: 3 and a half hours at least.




[Edited by Razor: Stickied]



4
Coding & Programming / Basic PHP (Lesson 1)
« on: 16 Dec 2009 - 07:04 »
This is my first tut on anything related to coding, but ill do my best to get the message across, as like you, i am also learning and improving my coding language.

-Note, it really helps to have at least a basic knowledge of html as well as JavaScript before working with php, although its necessarily required, you are going to need to learn it eventually.

---------------------------------

Introduction

For starters, lets get the basic facts and information out of the way.PHP Stands for PHP:Hypertext Preprocessor. It's a important and fairly easy language that can be used to write programs that output code (sort of like an html). However, anytime a website using php is accessed routines generate the markup to be downloaded to the browser. Although php may seem like a powerful and safe language, its actually a little risky. If a web page has multiple instances on it, they can intertwine with one another and cause the web page to fail. Other than slight security problems, PHP is one of the most studied and used scripting language to this day.

Another great thing about php is it goes right in with your html document, which means if your working on an html you can add php right into it, to a certain extent as long as you don't miss code anything. One reason PHP is so commonly used is for easier and quick solutions. If your trying to run a forum, its impossible to run it straight on html. With just plain html there would be nothing to hold data on, and it wouldn't constantly have to be personally edited with to have any changes.

Phew, glad thats out of the way.

Requirements
If your wondering how to even get started studying and working with php (obviously besides reading a book or a tutorial) well look no further!

First of all, i suggest the easier way to go about this, and find a webhost that supports php and mysql as well as a decent amount of FTP space.

I recommend www.000webhost.com as it has never failed me before, and it seems to almost never fail when it comes to wanting things done right.

However, if you dont wish to use a webserver and instead install it by yourself, look here. Although, even if your host does support php and mysql, you still need something to get acces to the ftp so that you can submit and practice your php on your new site. FileZilla is the best ftp up loader and editor so i recommend downloading it, if your interested click this

Lesson-1:Hello World!

Hurray! your finally going to test a simple yet important little php script. In this simple script, you'll learn what holds the key for all php to work, the two magic syntax that without them, php would not work. DUN DUN DUN DUN ARE YOU READY!

Code: [Select]
<?php
?>

yes thats it, i hope you werent expecting some huge line of code (Sorry if i killed your anticipation). ANYWAYS, yes those two little bits of syntax, can be added anywhere within the document, but must be exactly written like this, there is another way to write this:

Code: [Select]
<?
?>

But this is only for servers with shorthand support and its recommended to use:

Code: [Select]
<?php
?>

for the BEST compatability.

Now onto the script! It's the simply famous "Hello World!" script that starts you off by saying hello to the new world and adventures of coding, nicely wrapped in HTML as php usually is.

Code: [Select]
<html>
<body>

<?php
echo "Hello World";
?>


</body>
</html>

To test this out, simple open up notepad, paste this in their, click file>save as> helloworld.php .

Make sure you save it as a .php file or else it wont work. Once you save it, connect to your FTP server and upload the file into the database. Now go to your site e.g. www.hostylalala.lala.net/helloworld.php and if it worked you should get a nice "Hello World!" from your computer ^-^.

*REMEMBER! just because it has html markings in it does not mean it has to be saved as a .html, if you save it as a .html and it contains any php scripting, the php scripts will not run, and everything will be screwed untill you change the file extension.

But dont forget about that just yet, there are still some things we need to cover, lets break it down.

First we have:

Code: [Select]
<?php
This (As stated before) Is the starting code for any php script you wish to write.

Secondly:

Code: [Select]
echo
One of two basic elements used for output of text, in which this case was "Hello World!"

Third:

Code: [Select]
;
Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. This means when ever you want to end a code line add a (;) simple :).

Lastly:

Code: [Select]
?>
Use this to end the php script you are writing (simple).

Lesson-1:Comments

In PHP, we use // to make a single-line comment or /* and */ to make a large comment block
For PHP, to make a single line comment all you need to do is add a //. That will create a simple single line comment almost like hello world, yet written differently.

To make a large comment black, all you need to do is add a /* and */ before and after for a larger comment. E.G

Code: [Select]
<html>
<body>

<?php
//Mwuahahaha this be a comment arghhhhh

/*
this my good friends
is a super duper fantastic
comment block, hurry!
*/
?>


</body>
</html>

This is just another way to write something viewable to Users in different ways.

END OF LESSON-1

Summary

In these two lessons, you learned the fundamentals on how PHP works and what it is. You also learned how to write and format two simple php scripts. In the next lesson, i'll go over strings,variables, and a little information on mysql as a bonus for anyone who is interested.

---------------------------------

Well This is all i can give as of right now, its getting late and i think im going to hit the hay or maybe take my mind of coding for a bit because my head hurts! But its just so exciting hehe, anyways i hope this helps anyone who is looking or interested or even wants a little taste of the greatness PHP has to offer.


With Love, Ray.



[Edited title - added (Lesson 1) | Stickied - will sticky following lessons if they occur] ~Razor.

Pages: [1]