I figured some of you might want it. I thought that this post should go in this forum category instead of web development because this tutorial teaches the basics of general programming (using PHP as an example), not web design.
Note: This post was copied and pasted directly from my website. Also because I'm new to this forum, if I have broken any rules, please remove this topic if you wish (and contact me about what I've done).
Anyways..
Hi,
If you are interested in web development, or if you want to start learning a loosely typed programming language (less strict), you might want to consider PHP. PHP stands for PHP: Hypertext Processor, been around for years and years. Open source, and millions of web developers use it in combination with Apache and MySQL. Being popular means that there will always be lots of help on the internet, so use it to your advantage. The internet is your friend.
A portable web package (Apache + PHP + MySQL) I use is EasyPHP, and click here to download it. I assume that you can figure out how to install it properly. Make sure you install all the components just to be on the safe side. This tutorial requires that you have a basic understanding of computers, HTML, and common sense.
After you run it, test to see if it works by going to your browser and typing Rune-Server Official Pastebin. If that doesn’t work, type Rune-Server Official Pastebin. If only the latter works, you will have to remember that that is your address, not 80. You can always change your port in the Apache settings.
Start by visiting your EasyPHP installation folder, and view the "www" folder. That is the folder which all your files will be stored. Open your text editor (I recommend Notepad++ for good syntax highlighting) and write this code (this is the hello world program):
Save it in your www folder as index.php. Test by going into your browser and typing your address (localhost or localhost:8887). If it works, well, congratulations!Code:<?php
echo "Hello World!";
?>
The <?php and ?> are the starting and closing brackets of PHP. Anything else in between is the PHP code which it will process. PHP is case sensitive, meaning that "echo" and "Echo" aren’t the same. As you can see, echo is kind of like print. The printed text should be enclosed in single or double quotes. The semicolon at the end means to end the command.
Now with variables. I hope you know what they are. If not, think of Algebra. For example:
Yeah, in PHP variables start with the dollar sign. Don’t forget them! There are different types of variables, and the most basic ones in PHP are string (words, text, etc) and integers (numbers). Variables x and y are declared with the values 5 and 2, respectively. The variable z will have a result of 7, right? The final command is just printing the value of z. The dot in PHP in this sense is kind of like adding two objects together. The other basic arithmetic functions are * (multiply), - (subtract), and / (divide).Code:<?php
$x = 5;
$y = 2;
$z = $x + $y;
echo "Z is: " . $z;
?>
Example of text:
The next crucial basic factors of programming are conditional statements. If this, do this. If not, do that. For example:Code:<?php
$name = "Justin";
echo "My name is " . $name;
?>
And there you have it. This example demonstrates conventions. Like, you don't HAVE to tab a few spaces for the echo statements. But it is helpful when others look at your code (and you yourself too). There are many conventions in programming, and I may make a tutorial solely on conventions later on. I hope you can understand the syntax of the brackets by using common sense. If statements do not contain a semicolon because they are not actions. Functions, conditional statements, and many other parts in PHP are enclosed in brackets. When testing for equality, == is used, not =. This is a very easy mistake that many programmers make in any language.Code:<?php
$age = 17;
if ($age == 15)
{
echo "You are the same age as me.";
}
else if ($age > 15)
{
echo "You are older than me.";
}
else
{
echo "You are younger than me.";
}
?>
Comments are used to make code easier to read. They don't do anything, but it is good programming practice to include comments in your code so other people (and yourself) will be able to write and read your own notes.
Example of comments:
And there you go. Use comments whenever you can.Code:<?php
################
# I Like Comments
# by JZ
#
# March 26, 2012
################
$x = 17;
$y = 2;
/*
* below is a cool method
* which i really enjoy due
* to the fact that comments
* are cool and good looking
*/
function multiply($x, y)
{
return $x * $y; //this returns the product of argument 1 and argument 2
}
echo "The product is " . multiply($x, $y); //calls the multiply() function and echos it to the user
//end of program
?>
Next up, functions. Why use them? First off, they are are a good method to organize your code. Second, they are convenient to use. Instead of writing 100 lines for your so-and-so application and copying and pasting the same code, you can use functions. They are also very versatile.
Here's an example:
As you can see, functions can return a value. But they don't have to if you don't want them to return anything:Code:<?php
$firstNumber = 2;
$SecondNumber = 3;
function add($a, $b)
{
return $a+$b;
}
echo "The sum of " . $firstNumber . " and " . $secondNumber . " is " . add($firstNumber, $secondNumber);
?>
A common problem people face when using functions is the state of the variables. Variables declared in a function will not be able to be accessed outside of the function. What people commonly do is attempt to call a variable that was declared outside of a function without realizing that the variable's state is not global. To fix this, use the keyword global:Code:<?php
function displayBoldText($str)
{
echo "<b>" . $str . "</b>";
}
displayBoldText("This text should be bold.");
?>
In an earlier example, you should have observed the arguments inside the function. Arguments are very useful to provide data for your functions to use. For example, these are some built in functions that use arguments:Code:<?php
$myName = "Whac";
function displayMyName()
{
global $myName;
return $myName;
}
displayMyName(); //will result in 'whac' because it calls the global variable $myName
?>
Here’s a very good website: PHP Introduction to learn from.Code:<?php
$aString = "lmfao";
$s = strlen($aString); //in this case, a single string is the argument for this function
$con = mysql_connect("localhost", "root", $aString); //three arguments in this example, ip, user, and password
?>
More to come later.