Popular Posts

Friday, 2 July 2010

PHP basis programming

PHP basis programming


kindly visit www.apepoint.com for the original content of this blog.

Here in this chapter we will learn how to write simple php program and will also let you know various basis and helpful thing

In this chapter you will learn

First PHP script
Integration of PHP code in HTML CSS
Require and include
Date function
SERVER Varibles
How to run PHP

PHP is a server side programming language. So in order to run we need an installed server in our machine. There are so many servers are availbale we can download any in our system for example you can go for wamp(WINDOW APACHE MYSQL PHP) for window and on linux you can go for LAMP (LINUX APACHE MYSQL PHP)

After downloading and installing the server try to find the www folder in the installation directory for example in for WAMP it should be at c://wamp/www for linux /var/www/ and save your file in this directory and then open web browser and type the address

http://localhost/{ file-name}

For example you make file test.php and then open the web browser and type http://localhost/test.php in address bar this will display the out put of your script

As you can see the process is very simple in case any problem you can mail me at susheel@apepoint.com

Integrating HTML with PHP

In web we not only need to think about it's functionality but also about it's look s HTML , CSS are being most simple things to provide the looks on web and fortunely PHP provide very good support to these programming language these can be included at any time at any case

Below is an example which explain how H1 ta is can be included in PHP


echo "

Apepoint welcome you

";

?>

Here you saw how HTML is included in PHP to formate the out put this is very simple at any time we can include it there is one more example of this

Require and Include

Some time we need to write a specific code again and again for this as oter programming script language PHP provide function by using we can save our time. But in some case we need to write the same code in multiple pages thus this method does not help too much for this we have a very good solution of include/require

Here we are taking a very good example we need to type host name user name in password in all the time whenever we need to connect with database for example mysql in database specific applications consider in our application there are over 500 pages we can do so but what about the cases when we want to change the password in this case we will require to cahnge the password in all 500+ pages . So it is very tough task for any one. so for this we suggest to write a seperate file containging these details and include this in all files whenever require by syntax

include "config.php";

and when we require just make chnage in the single file it will make changes in all files
The same work can be done by using require function by below syntax
require "config.php";

Here their is different that in case file in finf in "include" case it will ignore and let script excute but in "require" case it will return fatel error and stop excution of script


file hi.php

echo "hi";
?>

another file

include "hi.php";

echo "susheel";

?>
DATE in PHP

There are several cases when we need to use date for example calendar based application, scheduling E-mail. Calendar is part of our life as it is part of our project for example in project of News PUBLISHING IT REQUIRE TO send news along with the date and when user visited our side it tell that it is 4 days old apart from this there are so many other needs We must recall a very standard thing here that in web technology there are so many data n time feature.



PHP have a great advantage over other script as in java script, Macromedia flash they fetch data time form the user's pc but the php fetch it from server. Thus it provides a most trusted date and time as it can be adjusted from server.



Jave script we can design some animated watch but if we provide time to these watch by PHP(through AJAX) then we can get these time form most updated



Before going through the details of this we wish to provide an example


$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
echo $today;
?>

The above script will print March 10, 2001, 5:16 pm here in this example you notice that Date object we passed F J Y g i a constants and get the date March 10, 2001, 5:16 pm in format so in case you just passed F only it will print March
if you passed j it will return 10 and so on here you notic that data and time printing is very simple in PHP By using require constant and date() we can get date and time in any format

Here we are giving one more example for this



// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the

$today = date("F j, Y, g:i a"); // return March 10, 2001, 5:16 pm
$today = date("m.d.y"); // return 03.10.01
$today = date("j, n, Y"); // return 10, 3, 2001
$today = date("Ymd"); // return 20010310
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // return it is the 10th day.
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // return 17:03:18 m is month
$today = date("H:i:s"); // return 17:16:18
?>



Here i a brief list of these constant
d
Day of the month, 2 digits with leading zeros
01 to 31

D
A textual representation of a day, three letters
Mon through Sun

j
Day of the month without leading zeros
1 to 31

l (lowercase 'L')
A full textual representation of the day of the week
Sunday through Saturday

N
ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
1 (for Monday) through 7 (for Sunday)

S
English ordinal suffix for the day of the month, 2 characters
st, nd, rd or th. Works well with j

w
Numeric representation of the day of the week
0 (for Sunday) through 6 (for Saturday)

z
The day of the year (starting from 0)
0 through 365




We suggest you to try one after another to under this.

No comments:

Post a Comment