PHP Tutorial
PHP Tutorial
You have to
HTML
JavaScript
Whats PHP?
PHP stands for Hypertext Preprocessor.
PHP scripts run inside Apache server or Microsoft IIS.
PHP and Apache server are free.
PHP code is very easy.
PHP is the most used server side scripting language.
PHP files contain PHP scripts and HTML.
PHP files have the extension php, php3, php4, or
phtml.
dynamic contents.
Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP Variables
As with algebra, PHP variables can be used to hold values (x=5) or expressions (z=x+y).
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)
Tip: echo is marginally faster compared to print as echo does not return any value.
PHP Strings
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
Example
<?php
$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!';
echo $x;
?>
PHP String Functions
In this chapter we will look at some commonly used functions to manipulate strings.
Tip: The position of the string "world" in the example above is 6. The reason that it is 6 (and not 7), is that
the first character position in the string is 0, and not 1.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can use
conditional statements in your code to do this.
In PHP we have the following conditional statements:
<input type="submit">
</form>
</body>
</html>
and "welcome_get.php" looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
The code above is quite simple. However, the most important thing is missing. You need to validate form
data to protect your script from malicious code.
GET vs. POST
Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This
array holds key/value pairs, where keys are the names of the form controls and values are the input data
from the user.
Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they
are always accessible, regardless of scope - and you can access them from any function, class or file
without having to do anything special.
$_GET is an array of variables passed to the current script via the URL parameters.
$_POST is an array of variables passed to the current script via the HTTP POST method.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
PHP Tutorial : PHP & MySQL
In this PHP Tutorial you will learn about PHP and MySQL - Connecting to MySQL, Closing a connection,
Selecting a database, Executing a query, Inserting data and Retrieving data.
Connecting to MySQL:
To connect to MySQL database server, use the function mysql_connect().
mysql_connect() takes three string arguments, the hostname, the username, and the password.
mysql_connect() returns an integer represents the connection index if the connection is successful, or
returns false if the connection fails.
Example:
<?php
$con = mysql_connect(localhost, john, smith);
echo $con;
?>
Closing a connection:
To close a connection to MySQL, use the function mysql_close().
mysql_close() takes one argument, which is the connection index.
Example:
<?php
$con = mysql_connect(localhost, john, smith);
echo $con;
mysql_close($con);
?>
Selecting a database:
Now you are connected to the MySQL server, to start using queries, you have first to select a database,
because MySQL server can contain many databases.
To select a database, use the function mysql_select_db().
mysql_select_db() takes two arguments, a string represents the database name, and the connection index.
Example:
<?php
$con = mysql_connect(localhost, john, smith);
mysql_select_db(MyDB, $con);
mysql_close($con);
?>
Executing a query:
Example:
<?php
$con = mysql_connect(localhost, john, smith);
mysql_select_db(MyDB, $con);
$query = SELECT first_name, last_name FROM Products;
$result = mysql_query($query, $con);
while($dataArray = mysql_fetch_array($result) {
$firstName = $dataArray[first_name];
$lastName = $dataArray[last_name];
echo $firstName $lastName<br />\n;
}
mysql_close($con);
?>