Showing posts with label start PHP. Show all posts
Showing posts with label start PHP. Show all posts

Nov 25, 2011

Learning PHP


PHP is the world's most popular web development language. Started by Danish-Greenlandic programmer Rasmus Lerdorf in 1995 it is now installed on more than 20 million websites and 1 million web servers and counting. 


It is estimated that for every 100 PHP developers, there are 42 Perl developers, 12 Python developers and 4 Ruby developers - PHPs popularity is the central reason why you should consider learning it above all others. 


PHP is the basis of Content Management Systems such as Drupal, Joomla and WordPress so gaining a knowledge of PHP would help you in using these scripts. 


Presumably your are already proficient with CSS and HTML and want to take your web creativity to another level. If you aren't, then stop right here. It's unthinkable to tackle PHP without a firm grounding in HTML and a good knowledge of CSS would be extremely useful. 


You don't have to have a complete knowledge of HTML in order to learn PHP but you certainly need to know the basics - the rest you will pick up in tandem with PHP. For instance, if you use Content Management Systems all the time you'll unlikely to be that familiar with coding forms, but HTML forms are an essential part of PHP and you'll need to be able to create them quickly and without fuss. 


Learning PHP is as hard as you can imagine it to be. You need time and lots of patience and preferably a reality you need to escape from for an inordinate amount of time. It's a good idea to pace yourself and set a two year framework in order to become familiar with the core of the language.


An interesting article here.

Jun 2, 2011

PHP - IF statement

Introduction

Over the past two parts I have shown you the basics of text in PHP and how to store it as variables. In this part of the tutorial I will show you how to use IF statements to make decisions in your scripts.

The Basics Of IF

If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.

IF Strucure

The structure of an IF statement is as follows:

IF (something == something else)
{
THEN Statement
} else {
ELSE Statement
}

Variables

The most common use of an IF statement is to compare a variable to another piece of text, a number, or another variable. For example:

if ($username == "webmaster")

which would compare the contents of the variable to the text string. The THEN section of code will only be executed if the variable is exactly the same as the contents of the quotation marks so if the variable contained 'Webmaster' or 'WEBMASTER' it will be false.

Constructing The THEN Statment

To add to your script, you can now add a THEN statement:

if ($username == "webmaster") {
echo "Please enter your password below";
}

This will only display this text if the username is webmaster. If not, nothing will be displayed. You can actually leave an IF statement like this, as there is no actual requirement to have an ELSE part. This is especially useful if you are using multiple IF statements.

Constructing The ELSE Statement

Adding The ELSE statement is as easy as the THEN statement. Just add some extra code:

if ($username == "webmaster") {
echo "Please enter your password below";
} else {
echo "We are sorry but you are not a recognised user";
}

Of course, you are not limited to just one line of code. You can add any PHP commands in between the curly brackets. You can even include other IF statments (nested statements).

Other Comparisons

There are other ways you can use your IF statement to compare values. Firstly, you can compare two different variables to see if their values match e.g.

if ($enteredpass == $password)

You can also use the standard comparision symbols to check to see if one variable is greater than or less than another:

if ($age < "13")

Or :

if ($date > $finished)

You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use:

if ($name == "" || $email == "" || $password == "") {
echo "Please fill in all the fields";
}

Part 4

In part four I will show you some other ways of using your PHP script to do other types of checks and loops.

Apr 21, 2011

PHP: POST & GET

PHP - POST & GET

Recall from the PHP Forms Lesson where we used an HTML form and sent it to a PHP web page for processing. In that lesson we opted to use the the post method for submitting, but we could have also chosen the get method. This lesson will review both transferring methods.

POST - Review

In our PHP Forms Lesson we used the post method. This is what the pertinent line of HTML code looked like:

HTML Code Excerpt:

This HTML code specifies that the form data will be submitted to the "process.php" web page using the POST method. The way that PHP does this is to store all the "posted" values into an associative array called "$_POST". Be sure to take notice the names of the form data names, as they represent the keys in the "$_POST" associative array.
Now that you know about associative arrays, the PHP code from "process.php" should make a litte more sense.

PHP Code Excerpt:

$quantity = $_POST['quantity'];
$item = $_POST['item'];
The form names are used as the keys in the associative array, so be sure that you never have two input items in your HTML form that have the same name. If you do, then you might see some problems arise.

PHP - GET

As we mentioned before, the alternative to the post method is get. If we were to change our HTML form to the get method, it would look like this:

HTML Code Excerpt:

The get method is different in that it passes the variables along to the "process.php" web page by appending them onto the end of the URL. The URL, after clicking submit, would have this added on to the end of it:
"?item=##&quantity=##"
The question mark "?" tells the browser that the following items are variables. Now that we changed the method of sending information on "order.html", we must change the "process.php" code to use the "$_GET" associative array.

PHP Code Excerpt:

$quantity = $_GET['quantity'];
$item = $_GET['item'];
After changing the array name the script will function properly. Using the get method displays the variable information to your visitor, so be sure you are not sending password information or other sensitive items with the get method. You would not want your visitors seeing something they are not supposed to!

Security Precautions

Whenever you are taking user input and using you need to be sure that the input is safe. If you are going to insert the data into a MySQL database, then you should be sure you have thought about preventing MySQL Injection. If you are going to make a user's input available to the public, then you should think about PHP htmlentities.

Apr 12, 2011

Time and Date - PHP

PHP has the ability to dynamically generate the time and date. Using a simple line of code we are able to include this on our site, however it is important to know how the formatting works.



You can use the date function in conjunction with the time function to display this in the format of date ( format , time ) In our case we want the start time to be now, so we will call the time first. We will demonstrate many different types of formatting


 $b = time (); 
 print date("m/d/y",$b) . "
"; 

 print date("D, F jS",$b) . "
"; 

 print date("l, F jS Y",$b) . "
"; 

 print date("g:i A",$b) . "
"; 

 print date("r",$b) . "
"; 

 print date("g:i:s A D, F jS Y",$b) . "
"; 

 ?> 


There is different formats that can be used in the date feature. Below is a summary of the variable used in date, and what each does. They ARE CaSe sEnsItIVe:


DAYS 
d - day of the month 2 digits (01-31) 
j - day of the month (1-31) 
D - 3 letter day (Mon - Sun) 
l - full name of day (Monday - Sunday) 
N - 1=Monday, 2=Tuesday, etc (1-7) 
S - suffix for date (st, nd, rd) 
w - 0=Sunday, 1=Monday (0-6) 
z - day of the year (1=365)


WEEK 
W - week of the year (1-52)


MONTH 
F - Full name of month (January - December)
m - 2 digit month number (01-12) 
n - month number (1-12) 
M - 3 letter month (Jan - Dec) 
t - Days in the month (28-31)


YEAR
L - leap year (0 no, 1 yes)
o - ISO-8601 year number (Ex. 1979, 2006)
Y - four digit year (Ex. 1979, 2006)
y - two digit year (Ex. 79, 06)


TIME
a - am or pm
A - AM or PM
B - Swatch Internet time (000 - 999)
g - 12 hour (1-12)
G - 24 hour c (0-23)
h - 2 digit 12 hour (01-12)
H - 2 digit 24 hour (00-23)
i - 2 digit minutes (00-59)
s 0 2 digit seconds (00-59)


OTHER e - timezone (Ex: GMT, CST)
I - daylight savings (1=yes, 0=no)
O - offset GMT (Ex: 0200)
Z - offset in seconds (-43200 - 43200)
r - full RFC 2822 formatted date

Basic PHP Syntax video

http://video.about.com/php/Basic-PHP-Syntax.htm

Aug 4, 2010

MySQL Database and PHP

MySQL Database and PHP


Almost all websites have content which is dynamic. I mean content is into database and PHP extract content from there.
The most used database on the internet is MySQL. Mysql is free and is the quickiest database on the web.

You can download Mysql Database from mysql.com


Mysql and PHP for beginners
This article assumes a few things.

You know how to use PHP. If you don't, the best place to start is by reading Welcome to PHP
You know how to run basic queries in a database. If you don't, I suggest you visit Getting started with SQL or parts 1 and 2 of Introduction to Databases for the Web
You have access to a server (or servers) running PHP and MySQL. If yuo don't, you can get hold of the software, for free, along with installation instructions at the PHP site and the MySQL site.
The server running PHP can connect to the server running MySQL. Test this from the command line first. If you can't connect without PHP, you're not going to be able to connect with PHP. If you're using other servers, you may need to ask the systems administrator for help
There's an existing database and table already running on MySQL, which we'll use with the PHP scripts. The database is called first_test, and has a table called people with some data in it. Run the following SQL to create and populate the table:
mysql> CREATE DATABASE first_test;
Query OK, 1 row affected (0.31 sec)
mysql> USE first_test;
Database changed
mysql> CREATE TABLE people (
id int UNIQUE NOT NULL,
first_name varchar(40),
surname varchar(50),
PRIMARY KEY(id)
);
Query OK, 0 rows affected (0.24 sec)
mysql> INSERT INTO people VALUES(1,'Ann','Brache');
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO people VALUES(2,'Narcizo','Levy');
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO people VALUES(3,'Tony','Crocker');
Query OK, 1 row affected (0.00 sec)
Your table should now look as follows:


mysql> SELECT * FROM people;
+----+------------+---------+
| id | first_name | surname |
+----+------------+---------+
| 1 | Ann | Brache |
| 2 | Narcizo | Levy |
| 3 | Tony | Crocker |
+----+------------+---------+
3 rows in set (0.19 sec)
Connecting

There are many MySQL functions in PHP. Perhaps you've taken a look at the official documentation and been intimidated by what looks like an endless task. But the good news is that to perform basic queries from within MySQL is very easy. This article will show you how to get up and running. Once you're comfortable with the basics, you can start to investigate the other functions, and you'll see that many of them are just duplicate ways of doing the same thing. Let's get started. The first thing to do is connect to the database.
All mysql functions begin with mysql_, so it comes as no surprise that the function to connect to MySQL is called mysql_connect. Let's assume you would connect to MySQL from the server you're running PHP with the following details:

Username pee_wee
Password let_me_in
From the command line, you'd type the following:
mysql -upee_wee -p
You'd enter the password once the prompt appears (you can also enter it directly on the command line, but get into the habit of doing it this way - it's more secure!). PHP can connect in the same way. The mysql_connect() function looks as follows:

resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]])

This function returns a resource which is a pointer to the database connection. It's also called a link identifier, or a database handle, and we'll use it in later functions.
Let's replace your connection details, and run this in a script:
<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
// you're going to do lots more here soon
mysql_close($dbh);
?>
All going well, you should see "Connected to MySQL" when you run this script. If you can't connect to the server, make sure your password, username and hostname are correct, and that you've copied the script exactly.

The last line of the script contains another MySQL function - mysql_close(). Although this isn't strictly speaking necessary, PHP will automatically close the connection when the script ends, you should get into the habit of closing what you open. If you start developing more serious applications, or move to other, less tolerant languages, you will find the transition more difficult if you haven't learnt the basics well from the beginning.

Once you've connected, you're going to want to select a database to work with. Let's assume the database is called first_test. To start working in this database (the equivalent of typing USE first_test in MySQL), you'll need the mysql_select_db() function.

bool mysql_select_db ( string database_name [, resource link_identifier])

To change to the first_test database, add the mysql_select_db() function call to your script, as follows:

<?php
$username = "pee_wee";
$password = "let_me_in";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
$selected = mysql_select_db("first_test",$dbh)
or die("Could not select first_test");
// you're going to do lots more here soon
mysql_close($dbh);
?>

Aug 2, 2010

PHP start


Here we will write our first PHP code. And, after that some phrases about PHP variables.


Before you can work with PHP you need to install PHP on your computer if you haven’t installed PHP yet, now is a good time to install PHP.

First we will make a simple HTML page




Welcome to our first PHP tutorial.
 


Okay the above HTML page doesn't do anything special but just outputs Welcome to PHP tutorial on the browser, now lets add some PHP tags to it.

Note: All PHP code is written between  and ?> tags, now lets write "Welcome to our first PHP tutorial" using PHP.







   echo "Welcome to PHP tutorial";

?>



Okay the above PHP script will output Welcome to PHP tutorial note that we use the echo construct of PHP, the echo function simply outputs the content to the browser.

So if we wanted to write "Welcome" we would write echo "Welcome";

Note Have you noticed the ; at the end of the statment, Each PHP instruction must end with a semicolon.


Introduction to Variables
Let me introduce you to PHP variables, variables help you store and retrive data.

  • All variables start with a dollar sign $


  • The name of the variable can consist of letters and numbers, but must begin with a letter. It can also contain special characters like underscore '_', see some examples below

    $site = 'phpbuddy.com' // valid variable
    $4site = 'not yet'; // invalid variable starts with a number
    $_4site = 'not yet'; // valid variable starts with an underscore 

    Some more examples

    $testvariable = 1 + 1; // Assigns a value of 2.
    $testvariable = 1 – 1; // Assigns a value of 0.
    $testvariable = 2 * 2; // Assigns a value of 4.
    $testvariable = 2 / 2; // Assigns a value of 1. 

    Okay to make things easier I will show you an example here we will create a variables $a, $b and multiply them and store the value in $c, just see it's so simple
    
    
    
    $a = 5;  //we created a variable a and assigned it a value of 5
    
    $b = 2;  //we created a variable b and assigned it a value of 2
    
    
    
    $c = $a * $b;  //we multiply varaible a and b and store the value in c
    
    echo $c;  //we print the content of variable c which is 10
    
    ?>
    


    PHP variables can hold strings, dynamic data, PHP variables are case sensitive $a is not the same as $A

    
    
    
    $a = "Welcome to PHP ";  //$a holds the string Welcome to PHP
    
    $A = 4;  //$A holds the value 4
    
    
    
    echo "Variable a conatains: $a";
    
    echo "Variable A contains: $A";
    
    
    
    echo "$a $A";  //outputs both variables $a and $A
    
    ?>