Php Nodes Full

Embed Size (px)

Citation preview

  • 8/8/2019 Php Nodes Full

    1/109

    [email protected] MySql, Php, Apache

    Unit-I: Installing and configuring MYSQL, Apache and PHP

    Chapter-1. Installing and configuring MYSQL

    Current and future versions of MYSQL:

    MySQL 5.5 is the current development release series. MySQL 5.1 is the current General Availability (Production) release series. MySQL 5.0 is the production release series. The MySQL is open source and the versions are availabl e on website is

    http://www.mysql.com/downloads . MYSQL AB, now owned by the sun micro systems, it is responsible for creating and

    distributing mysql.

    The MySQL distribution consist of three numbers and a suffix; for example, mysql-5.0.14-rc .

    The first number ( 5) is the major version and describes the file format. All MySQL 5releases have the same file format. The second number ( 0) is the release level. the major version and release level combines

    the release series number. The third number ( 14 ) is the version number or minor number within the release series.

    This is incremented for each new release.

    For each minor update, the last number is increased. When there are major new features with previous versions, the second number is incremented. When the file format changes, the firstnumber is increased.

    Release names also include a suffix. The possible suffixes are: alpha indicates that the release is for preview purposes only beta indicates that the release is appropriate for use with new development. rc indicates a Release Candidate. Release candidates are having all of MySQL's internal

    testing. If there is no suffix, it indicates that is the release is a General Availability (GA) or

    Production release.

    How to get mysql s/w: MYSQL AB company develops and maintains the mysql database server on website is http://www.mysql.com/downloads or http://dev.mysql.com .in this websites there are

    containing number of o/s platform distributions. Select the one distribution file in specific os. Youwill install the mysql on linux then you will choose to rpm extension or .tar.gz extension or youwill install on windows choose win32 binary distribution.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    1

    http://www.mysql.com/downloadshttp://www.mysql.com/downloadshttp://www.mysql.com/downloadshttp://www.mysql.com/downloads
  • 8/8/2019 Php Nodes Full

    2/109

    [email protected] MySql, Php, Apache

    Mysql installation on LINUX:

    Process of Installing mysql on linux you use two ways i.e RPM or Binaries

    RPM installation it is fast and simple installation The only drawback here is thatMySQL is not installed into a single destination it's install the several directories.

    Installing binaries downloading installation files from the mysql.com website, copyingthem into a directory of your choice, and doing some simple configuration.

    Using RPM Installation of mysql:

    Using RPM , it has contain two files:

    MySql- server-type -VERSIONNUMBER .PLATFORM .rpm----it is my sql s erver MySql- client-type -VERSIONNUMBER .PLATFORM .rpm----it is my sql client libraries.

    To perform a minimal installation from RPMs, you can type the following at your Linux prompt

    i.e

    Shell>rpm i MySQL server-5.0.51.i386.rpm MySQL client 5.0.51.i386.rpm

    Now mysq linstallation will be started on linux..

    Using Binaries Installation of mysql:

    Another installation method is to install MySQL from binary. This method requires gunzip and tar files (files with .tar.gz extension) and also requires to create groups and users on the system.

    1. If your system does not contain a user and group for mysql then u will create user andgroups as follows

    shell> groupadd mysql

    shell> useradd -g mysql mysql

    The above will create a user that has login permissions to your server.These commands addthe mysql group and the mysql user.

    2. Pick the directory under which you want to intsallation files and change location into itshell> cd /usr/local

    3. Unpack the distribution, which creates the installation directory. Then create a symbolic link tothat directory:

    shell> gunzip < /path/to/mysql-VERSION-OS .tar.gz | tar xvf -

    shell> ln -s full-path-to-mysql-VERSION-OS mysql

    shell> tar zxvf /path/to/mysql-VERSION-OS .tar.gz

    S.G.S Degree College, pippara T.Ganesh M.C.A

    2

  • 8/8/2019 Php Nodes Full

    3/109

    [email protected] MySql, Php, Apache

    4. Change location into the installation directory:

    shell> cd mysql

    You will find several files and subdirectories in the mysql directory i.e for installation purposesare the bin and scripts subdirectories:

    The bin directory contains client programs and the server.The scripts directory contains the mysql_install_db script. It is used to initializethe mysql database

    mysql installation files are contains as root so change the owenership to mysql by usingfollowing commands.

    shell> chown -R mysql .

    shell> chgrp -R mysql .

    The first command changes the owner attribute of the files to the mysql user. The secondchanges the group attribute to the mysql group.

    If you have not installed MySQL before, you must create the MySQL data directory andinitialize the grant tables:

    shell> scripts/mysql_install_db --user=mysql

    this command runs as root , so change ownerships to root because data directory and itcontents with mysql as the owner.

    After creating or updating the grant tables, you need to restart the server manually.

    7. Most of the MySQL installation can be owned by root .this, run the following commands asroot in the installation directory:

    shell> chown -R root .

    shell> chown -R mysql data

    8. After everything has been unpacked and installed, To start the MySQL server, use thefollowing command:

    shell> bin/mysqld_safe --user=mysql &

    After starting the server, you should set up passwords.

    set the root password

    Now, before you do anything else, set root user's password (!)

    SELECT Host, User FROM mysql.user;SET PASSWORD FOR 'root'@'localhost' = PASSWORD(' new_password ');

    S.G.S Degree College, pippara T.Ganesh M.C.A

    3

  • 8/8/2019 Php Nodes Full

    4/109

    [email protected] MySql, Php, Apache

    MYSQL installation on windows :

    1. If you installed mysql software on windows then you can download the MySQLServer(win32 installer) software from the MySQL website . after download it stores the fileon your Windows Desktop.

    2. now double click the setup.exe file. It will be display the mysql setup wizard as shown in

    below figure. In this window select the next tab.

    3. after clcik next it will display the setup type window. It provides 3 setup type options i.etypical, custom, complete. Now here Choose the custom type option and click the nextshown in below fig.

    4. next it displays custom setup window. Here change the all features are installed andchange the installation directory i.e c:\server\mysql\ and click the next tab.(shown in

    below fig)

    S.G.S Degree College, pippara T.Ganesh M.C.A

    4

    http://dev.mysql.com/downloads/mysql/4.1.htmlhttp://dev.mysql.com/downloads/mysql/4.1.htmlhttp://dev.mysql.com/downloads/mysql/4.1.html
  • 8/8/2019 Php Nodes Full

    5/109

    [email protected] MySql, Php, Apache

    5. after clcik the next tab the wizard ready to began installation when e ver click the install tab.(as shown in below fig)

    6. click the install tab now it installs the files of mysql server in proper location.

    7. When the installation is complete it display the wizard complete window. In this windowselect the configure option and click the finish tab.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    5

  • 8/8/2019 Php Nodes Full

    6/109

    [email protected] MySql, Php, Apache

    8. when ever click the finish tab it displays the configuration window welcome screen, click the next.(as shown in below fig)

    9. now go to the next wizard. you will see two options for s erver configuration i.e detailedconfiguration, standard configuration options. You can select the detailed configuration

    option and click the next as shown in below fig

    10. display the next wizard, in this wizard set the windows options i.e select the intsall aswindows service and give the service name i.e mysql in service name textbox and select theinclude bin directory in windows path option , click the next.(as shown in below fig.)

    S.G.S Degree College, pippara T.Ganesh M.C.A

    6

  • 8/8/2019 Php Nodes Full

    7/109

    [email protected] MySql, Php, Apache

    11. next wizard is displayed. Here set the security settings i.e type the new root password andtype the confirm password, click next as shown in below fig.

    12. now ready to execute all settings to the configuration whe n ever u click the execute button.select the execute button, now create the configuration file and start the mysql service.

    Click finish tab to close the wizard.

    Introducing the mysql privilage system:The mysql privilage system is always on. You can try to connect the mysql server it checks thefollowing three things i.e

    check the host(where you access from)Username and passwordCommand privileges

    All this information is stored in database it is automatically created when mysql is installed.

    There are number of privileges in mysql database i.eColumns privileges: defines user privileges for particular fields in a tableDb privileges: defines the permissions for all databases on a server Host privileges: defines the valid hosts that is used to connect the database.Procs privilages: defines user privileges for stored routines.Tables privileges: defines the user privileges for specific tables within the databaseUser: defines the command privileges for a specific user.

    The Two-step Authentication process:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    7

  • 8/8/2019 Php Nodes Full

    8/109

    [email protected] MySql, Php, Apache

    You are connecting mysql from host by using username and password. Username and password is correct then we will moves to second step otherwise your host can notconnect to server.If Username and password correct then Mysql verifies to sql command privileges thatyou are accessing table ,field or database.

    if step 1 fails then it displays the error but you want be able to continue the step2. for example,

    suppose that you are connecting to mysql with username of jyothi and password ab123. it displaysthe error message if connection variables is incorrect for any following reasons:

    Password is incorrectUsername jyothi does not exist.User jyothi can connect from localhost but cannot use t he mydb database.

    Example of error like following:Shell>mysql h localhost u jyothi pab123 testError 1045: Access denied for user:jyothi@localhost (password: YES)

    If user jyothi with password ac123 is allowed to connect the database, mysql checks the actions

    that jyothi can perform step2. here mysql allows the jyothi to select the data but it not allowed toinsert data.Examples:Shell> mysql h localhost u jyothi pab123 testReading tables information onlyWelcome to the mysl monitor.Your mysql connection Id is 12 to server version:5.0.20 Mysql>select * from jyo_table;

    ID Name1

    2

    Nalini

    kumari

    MySql>insert into jyothi values (,my test);Error1044: Access denied for user.

    Working with user privilages:if you are the administrator of your mysql server and you are the provide the privileges to users toaccessing the database by using grant command and revoke commands.Adding Users:Grant command is used to adding new users by connecting mysql database and accessing themysql database. the user set the privileges to access the db in particular database using grant

    command.Syntax of the grant command:Grant privilegesOn databsename.tablenameTo username@hostIdentified by password;

    List of privileges provide to particular user to access the database.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    8

  • 8/8/2019 Php Nodes Full

    9/109

    [email protected] MySql, Php, Apache

    All----------- all common privileges gives to the user Alter- ------user can modify the tables,columns and indexes.Create------ create the tables and databasesDelete------ user can delete the records from tables.Drop- ------drop the tables and databasesInsert- -----user can insert the records to the table.

    Select- -----user can select the records from tables.Update- ---user can updates records in table.

    Suppose you want to create a user i.e user name is ravi with password of abc123 and provides aselect, insert privileges on emp table in employee database. The us er able to connect the database ,accessing the database using this command.

    Grant select, insertOn employee. empTo ravi @ localhostIdentified by abc123;Ex:

    Shell> mysql h localhost u jyothi pab123 testMysql> select * from emp;The query is accepted and display this

    EID Ename12

    Nalinikumari

    Mysql> insert into emp values (3,bhanu);The query is accepted.Mysql> drop emp;Access is denied drop is not used.

    Suppose only perform select the privileges on all table in all database like this query.

    Grant selectOn *.*To ravi@%Identified by abc123;

    Note: * means access all tables on all databases and % means access all localhosts.

    It perform all the privileges on all table in all database connecting using any local host like this ,

    Grant AllOn *.*To ravi@%Identified by abc123;

    Removing the privileges:

    Removing privileges instead of the grant command, you can use revoke command.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    9

  • 8/8/2019 Php Nodes Full

    10/109

    [email protected] MySql, Php, Apache

    Syntax:Revoke privilegesOn databasename.tablenameFom username@hostname

    for example the user ravi contains privileges is insert and select in all tables on employeedatabase , you can removing the insert privileges by using revoke command Revoke insertOn employee.*Fom ravi@%;

    Ex: Mysql> insert into emp values (4,kumari);Query not accepted.(because insert privilage is removing)Suppose we can remove all privilages on all tables in mysql database s. The command isRevoke AllOn *.*Fom ravi@%;

    S.G.S Degree College, pippara T.Ganesh M.C.A

    10

  • 8/8/2019 Php Nodes Full

    11/109

    [email protected] MySql, Php, Apache

    Chapter-2. Installing and configuring Apache

    Current and future versions of Apache: If u want to download the apache httpd server then you have to visit website athttp://httpd.apache.org . in this website there are several apache versions releases i.e apache 1.3.xand Apache 2.0.x and Apache 2.2.X versions. Here x indicates different version numbers. TheApache 2.2 release series has contains different version numbers i .e Apache 2.2.1 and Apache2.2.2 etc. if u want to change or downloads the apache versions , go to visit apache website i.e thedownload link is http://httpd.apache.org .Choosing appropriate installation method:Apache is a open source. Open source means every one can access the full source code of thesoftware. When you visit apache software foundation website, it is contains several options of apache installation s/ws. suppose apache server runs on Linux/Unix platform then u better tochoose apache binary distributions. The list of binary distributions isHttpd-2.2.3.tar.gz or httpd 2.2.9.tar.gz or httpd- 2.3.4.tar.gzSuppose if u want to run apache server on windows, we are go to apache website chooseinstallation method is (MSI Installer) win32 binary distribution. (Or ) directly download the apachewin32 distribution by using http://www.apache.org/dist/httpd/binary/win32/readme.html andthe lsit of windows binaies isHttpd-2.2.3.win32.vc6.x86 or httpd 2.3.2.win32.vc6.x86 etc.

    Installing Apache on windows:-

    1. download the apache server from apache website in internet. this software is a freesoftware not purchased because apache is open source.

    2. the apache website is httpd.apache.org. in this website select the one windowsdistribution file.

    3. after download the apache s/w just go to file location in your local system.4. double click the apache download file to start the installation process.5. it displays the windows installer welcome screen as shown in below fig.

    6. click next to continue the installation process and next you will accept apachelicense agreement, click next.

    7. after accept the license , the installer presents you with brief introduction to apache.That is it provides basic information about your computer, as shown in belowfigure.

    8. it includes network domain(network address) for the server and name of the server,administrator email address. Select the option is for all users, on port as a service.this option refers to apache server starts manually when windows will start. Click next.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    11

    http://httpd.apache.org/http://httpd.apache.org/http://www.apache.org/dist/httpd/binary/win32/readme.htmlhttp://www.apache.org/dist/httpd/binary/win32/readme.htmlhttp://httpd.apache.org/http://httpd.apache.org/http://www.apache.org/dist/httpd/binary/win32/readme.html
  • 8/8/2019 Php Nodes Full

    12/109

    [email protected] MySql, Php, Apache

    9. the next screen enables to choose the type of installation: it displays two options i.etypical and custom.

    10. you can select custom option click next. Here change the apache installationdirectory i.e c:\server\Apache2 and click next.

    11. after click next , it proceeds installation process.

    12. successfully installed apache server ,it displays the installation wizard completedscreen, here click the finish.

    13. now the apache server runs on your system manua lly. Check The apache server runs successful or not, just doing following i.e open the browser gives thehttp://localhost request. it displays the its work, that means server workingsuccessfully.

    Installing Apache on linux/unix:Successfully install the Apache on linux it follows genrel steps i.e

    Download the softwareRun the configurationInstall the software

    Download the software:The downloading the apache source code from http://httpd.apche.org/download.cgi website. Inthis website there are number of distribution files with tar and gzip(compressed file). Nowdownload the one of the file is httpd-2.2.verion.tar.gz in apache website, here version is mostrelease of Apache. For example , Apache version 2.2.8 is distributed as a file named http://httpd 2.2.8.tar.gz. store the download file in a directory for source files such as /usr/local/src/.Uncompressing code and Run the configuration:You can uncompress the software by using following command.

    Shell>gun zip < httpd-2.2.8.tar.gz | tar xvf

    Configuring Your Server for Apache: Once you have got the files, you need to tell your machine where to find everything byconfiguring the source files. i.e to accept all the defaults and type the following:

    Shell> ./configure

    To configure Apache to install itself in aspecific location to /usr/local/apache/ and enable the useod mod_so, the following command is

    Shell> ./configureprefix=/usr/local/apach--enable-so..Configure ok

    Installing apache:after configure or compilation is finished, you can install the Apache by using make command atthe linux prompt. i.e type the following command is installed the apacheshell>make install..

    S.G.S Degree College, pippara T.Ganesh M.C.A

    12

    http://localhost/http://httpd.apche.org/download.cgihttp://httpd.apche.org/download.cgihttp://httpd.apche.org/download.cgihttp://localhost/http://httpd.apche.org/download.cgi
  • 8/8/2019 Php Nodes Full

    13/109

    [email protected] MySql, Php, Apache

    Installing header filesInstalling system files.Make[1]: instlling directory usr/local/apache/bin/httpd-2.2.8.To test the apache installation successfully or not ,type the following at the promptShell> /usr/local/apache/bin/httpd vThe output isServer version: Apache/2.2.8Server built: july 31 2010 10:34:11

    Apache Log Files:

    The Apache includes three log files by default. i.e

    Install_logfile Access_logfile Error_logfile

    The install log file contains the apache server installation information. The access_log file is for tracking client requests. The error_log file is for recording important events, such as errors.Install_log file:This file includes entire server information at the time of installation i.e server name, server root,server port, domain name etc.Access_log file:This file stores server accessed information. when a client requests a file from server. Apacherecords information with request, IP address, HTTP status code and time of access. Shows samplelog file entries like as below

    127.0.0.1 - - [08/Aug/2010:22:36:52 +0530] "GET / HTTP/1.1" 200 44

    127.0.0.1 - - [08/Aug/2010:22:54:10 +0530] "GET /phpinfo1.php HTTP/1.1" 404 210

    The first line indicates access the server at first time and second line indicates accessed the phpfile.Error_log file:The error_log file includes error messages, startup messages. Suppose the client gives the requestto server i.e execute h1.php. but server does not contain this file it provides the error informationfrom error log file. Shows as below error_log entries.

    [Wed Aug 25 21:21:02 2010] [notice] Child 332: Starting thread to listen on port 80.

    [Wed Aug 25 23:46:11 2010] [error] [client 127.0.0.1] script 'C:/server/apache2/htdocs/hi.php'not found or unable to start

    Apache Related Commands:

    The apache server executes and stops also by using commands. The apache server has containsome apache related commands i.e

    S.G.S Degree College, pippara T.Ganesh M.C.A

    13

  • 8/8/2019 Php Nodes Full

    14/109

    [email protected] MySql, Php, Apache

    [Note: the apache commands are executes the httpd executable file. The httpd file is containsc:\server\apache\bin\. So execute all commands in c:\server\apache\bin.]-k install : this command is used to install the apache service.

    Syntax: httpd -k installExample : c:\server\apache2\bin>httpd -k install

    Apache service is started

    -k start : this command is used to start the apache server successfullySyntax: httpd -k startExample : c:\server\apache2\bin>httpd -k start

    Apache server starting

    -k stop : this command is used to stop the apache server successfullySyntax: httpd -k stopExample : c:\server\apache2\bin>httpd -k stop

    Apache server stoping

    -k restart : this command is used to restart the apache server successfullySyntax: httpd -k restartExample : c:\server\apache2\bin>httpd -k restart

    Apache server restarted

    -k uninstall : this command is used to uninstall the apache serviceSyntax: httpd -k uninstallExample : c:\server\apache2\bin>httpd -k uninstall

    Apache service is successfully uninstalled.

    -l : this command is used to display the list compiled modulesSyntax: httpd -lExample : c:\server\apache2\bin>httpd -l

    -v : this command is used to display the version number of apache server.Syntax: httpd -vExample : c:\server\apache2\bin>httpd -v

    Version number : Apache /2.2.2

    -L : this command is used to display the list of configuration directivesSyntax: httpd -LExample : c:\server\apache2\bin>httpd -L

    S.G.S Degree College, pippara T.Ganesh M.C.A

    14

  • 8/8/2019 Php Nodes Full

    15/109

    [email protected] MySql, Php, Apache

    Chapter-3. Installing and configur ing PHP

    Building PHP on Windows with Apache:

    1) if u want to installing the php on your windows system , now you have to go to the phpwebsite and download the php distribution files. i.e the website link ishttp://www.php.net/downloads .

    2) After download the php zip file, now go to download folder in your system to find the fileis called php-5.2.3-Win32.zip.3) Extract the content of this file to C:\server\php . Now all of the files from the php-5.2.3-

    Win32 folder and place them directly in C:\server\php .4) After extract the files, set the php path to operating system variable. Now we need to add

    C:\server\php to Environment VariablesPath setting procedure:

    I. right click on My Computer icon on your desktop and choose Properties . Thenchoose Advanced tab and click Environment Variables button (like as below fig).

    II. Now select Path from the System variables list in the bottom part of the window andclick Edit tab (below fig)

    S.G.S Degree College, pippara T.Ganesh M.C.A

    15

    http://www.php.net/downloadshttp://www.php.net/downloads
  • 8/8/2019 Php Nodes Full

    16/109

    [email protected] MySql, Php, Apache

    III. In the new window click inside the Variable value: box and press End button onyour keyboard. Now type the c:/server/php after put the semicolon.

    IV. Click the ok buttons until close the system properties windows. And restart thecomputer

    5) Next step will be to find out PHP is working or not, here just Go to Start -> Run and typecmd . Now Display the command prompt, In this command prompt window type php -v . if success fuly installed it displays the php version number otherwise none.

    6) Next step change the configuration settings of php. Now open php configuration file. i.ego to c:\server\php here find out the php.ini recommended file.

    7) Rename that file to php.ini . Open this file and make the following changes like as below:I. identify line with doc_root = and change it to:

    doc_root = C:\webserver\Apache2\htdocs II. identify line with extension_dir = "./" and change it to:

    extension_dir = C:\webserver\php\ext 8) save and close the file.9) Next step change the Apache configuration settings. Now open the Apache configuration

    file i.e go to c:\server\Apache2\conf here open the httpd.conf file and changes like as belowsections.

    I. identify LoadModule section and here add the following line isLoadModule php5_module "C:\server\php\php5apache2_2.dll"

    II. identify AddType section and add the following line isAddType application\x-httpd-php .php

    III. lastly place the following line i.ePHPIniDir "C:\server\php\"

    10) save and close the apache configuration file. Now restart the apache server.Testing your installation:

    To test php installation is to create a small test script by using phpinfo() function. Thisfunction will display the list of configuration information. Open text editor type the followingline i.e

    S.G.S Degree College, pippara T.Ganesh M.C.A

    16

  • 8/8/2019 Php Nodes Full

    17/109

    [email protected] MySql, Php, Apache

    Save the file as phpinfo.php into c:\server\apache2\htdocs. next start the server and open the browser and type the following URL:http://localhost/phpinfo.php .[->You should see now a PHP information page (like as below fig)]

    S.G.S Degree College, pippara T.Ganesh M.C.A

    17

  • 8/8/2019 Php Nodes Full

    18/109

    [email protected] MySql, Php, Apache

    UNIT-IIPHP BASICS

    S.G.S Degree College, pippara T.Ganesh M.C.A

    18

  • 8/8/2019 Php Nodes Full

    19/109

    [email protected] MySql, Php, Apache

    Chapter-1. Basics of PHP

    Introduction of PHP:

    PHP stands for personal home page it is implemented RASMUS LERDORF , 1995 using Cand Perl languages. It is used to develop the dynamic web applications.

    The first version of php is php 1.0/F.I (F.I means form inte rpreter)It is server side scripting language and it has alias name is hypertext preprocessor.Php 1.0 to upto php 4.0 not contains O.O.P features. And php 5.0 version completelyimplement O.O.P features.

    Java is not user friendly but php is user friendly.

    Features of PHP:

    Php is cross plat form.Php is cross server(it means supports different types of servers).Php is open source technology.Php is extensibleWe can easily embedded VB,JSP,ASP scripts to php script.Php 5.0 is providing complete object oriented structure.Php is providing different types of security modes like authentication, authorization,encryption etc.

    Php supports any type of databases.Php supports light weight editors to implement logic.

    Php is very easy to learn and develop the applications.

    Client side script:-The script is which is executing client machine is called as client side script. Client side script isexecuting with the help of browser. Every browser contains interpreters to execute the client sidescript.Ex: HTML, Java Script, XML.Server side script:The script which execute server sends the executable code to the browser.Ex: PHP,ASP,JSP etc.Web server:Web server is a software used to run the web applications. It handles the requests from client.Ex: Apache, IIS, Tomcat etc.Web browser:It is software used to access the websites from web server. Ex: internet explorer, mozilla etc.Protocols:It is a set of instructions and transfer the information from one system to another system.Ex: TCP/IP,http.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    19

  • 8/8/2019 Php Nodes Full

    20/109

    [email protected] MySql, Php, Apache

    Request: a trip of web page from client to server is called request.Response: server sends the output to the client in the form of response.

    Versions of PHP:

    Php 1.0: this is the basic version of php developed by rasmus lerdorf in 1995 it is used to find out the hit count how many getting to the web server. It is not a server side scripting language. It is used to handle the server side request.

    Php 2.0: Php 2.0 is released in 1997. it is introduced as server side s cripting language . Collection of logins , password access It supports different database servers. It supports file uploads and many functions.

    Php 3.0: the php 3.0 version is developed by Andi Gujmanj, suraski in 1998. It is completely server side scripting language. The Alias name of php is hyper text preprocessor provided from these version. It supports multiplatform. It is cross platform. It supports email functions.

    Php 4.0: This version relased by zend engine in 2000. It is cross platform and cross server.

    This version include some advanced features like http sessions, supports flash,XML, pdf etc. it contains smarty templating system. Which is used to develop web applications

    very fast.Php 5.0:

    Php 5.0 relasd in 20005, zend engine Php 5.0 contains complete object oriented programming structure. It supports

    inheritance, abstraction, access specifiers, encapsulation etc. It supports the exception handling concepts. Supports web services and XML support.

    [Note: some important points of php script: every php script extension should be .php or

    .phtml. every php statement ends with semicolon. ]

    The basics of PHP scripts :A php scripting block always starts with . A php scripting block can be

    placed anywhere in the document.Syntax of php script:

  • 8/8/2019 Php Nodes Full

    21/109

    [email protected] MySql, Php, Apache

    ..

    ?>

    A sample php script:

    Save the script i.e hello.php from c:/server/apache2/htdocs/. Next start the server and open the

    browser give the request to server i.e http://localhost/hello.php shown below fig.

    Beginning and ending a block of php statements: (or) types of php style tags:

    The php engine identify the php block with php style tags. Php contains special style tags that isused to implement the php script. Every php script contains beginning and ending blocks. there arefour types of style tags. i.e

    1) Standard tags:The standard start tag is . the implementing of php script by usingstandard tags is shown in below

    2) Short tags:The short start tag is . implementing of php code by using short tagsalso shown in below codeNote: the short tags are directly not executed. We can go to php.ini configuration here findshort tags and change the short_open_tag is set to on.i.e short_open_tag=on;

    3) Asp tags:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    21

    http://localhost/hello.phphttp://localhost/hello.phphttp://localhost/hello.php
  • 8/8/2019 Php Nodes Full

    22/109

    [email protected] MySql, Php, Apache

    The Asp start tag is . implementing of php code by using Asp tagsalso shown in below codeNote: the Asp tags are directly not executed. We can go to php.ini configuration here findAsp tags and change the Asp_tags is set to on.i.e asp_tags=on;

    4) Script tags:The script start tag is And end tag is . implementingof php code by using script tags also shown in below code

    echo hello short tags;

    The Echo statement and print() function: (or) ( output functions in php):

    Print(): print() is a function. This function display the data on webpage and returns a true or falsevalues. By using print function we can print only one statement.Example:Output: hello php

    Echo statement: echo is a statement . this statement is used to print the multiple statements atonce. It does not return any values. The echo statement performance is faster than print().Example:Combining HTML and PHP:

    The simply adding the HTML tags outside of PHP start and end tags, as shown in below example.(or) the html tags are must be enclosed with double quotes in php statements.

    Example:

    welcome

    S.G.S Degree College, pippara T.Ganesh M.C.A

    22

  • 8/8/2019 Php Nodes Full

    23/109

    [email protected] MySql, Php, Apache

    Save: c:\server\apache2\htdocs\welcome.phpStart the server and Open browser give the request i.e http://loclhost/welcome.php

    Output:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    23

    http://loclhost/welcome.phphttp://loclhost/welcome.phphttp://loclhost/welcome.php
  • 8/8/2019 Php Nodes Full

    24/109

    [email protected] MySql, Php, Apache

    Chapter-2. The Building blocks of PHP

    the building blocks of php is Variables Datatypes Operators Constants

    Variables:Variables are used to store the values. All variables in PH P start with a $ sign symbol. Suppose wecan forget $ sign symbol at the beginning of variable declaration it will not work.The syntax of variable declaration:$var_name = value;

    Php is loosly typed language because the programmers did not provide the data types at the timeof variable declaration.

    Example of creating a variable containing a string, and a variable containing a number:

    Output: hello world16

    Naming rules of varables:

    A variable name must start with a letter or an underscore "_".A variable name should not contain spacesWe can also declared the lower case letters or upper case letters.A variable name should not contain any special symbols.

    Types of variables in PHP:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    24

  • 8/8/2019 Php Nodes Full

    25/109

    [email protected] MySql, Php, Apache

    Local variablesGlobal variablesStatic variablesSuper global varables.

    Local variables: A variable declaration with in the function that variable scope is onlywith in the function.

    Ex: Global variable: A variable declaration in globa l. The scope of variable is entire the script.Php we cant access global variables directly from a function by using global keyword wecan access the global variables.Ex :

    Static variables: static is keyword. A vaiable is preceded by static keyword that variable iscalled static variable. Assign the values into the Static variables only one time.

    Ex:

  • 8/8/2019 Php Nodes Full

    26/109

    [email protected] MySql, Php, Apache

    $_POST: this variable is used o access the post method posted values. $_REQUEST: this variable is used to access the get, post method posted values. $_COOKIE: this variable is used to get the values from cookie. $_SESSION: It is used to read the values of session variables. $_FILES: this is used to access the uploaded file information.

    Data types in PHP:

    Php supports 3 types of data types i.e

    Scalar data typesCompound data typesSpecial data types.

    Scalar data types: All primitive data types is called scalar da ta types. It contains Boolean,integer, float or double, string.Boolean: this data type represent true or false.Ex: Is_bool(variable name): this function is used to test the given variable stores the Boolean or not.Suppose the given variable stores the Boolean data it returns one otherwise it returns none.

    Ex:$a=true;Echo is_bool($a);//it prints 1

    Integer: integer variables can holds numbers. i.e {.,-2,-1,0,1,2,.}Ex: $a=100;//it is integer varable.

    Is_int(variablename): it returns 1 (true) the variable is integer data type.Ex: $a=100;

    Echo is_int($a);//prints 1Float: floating numbers are contains fractional parts i.e (3.57,22.90 etc)

    Ex: $a=35.4(note: $a is contains float data type)Is_float(variable): it returns one (true) when the variable stores the float value.

    Ex: $a=89.9;Echo is_float($a);//it prints one

    String: sting variable can hold collection of characters with in the double quotes or single quotes.Ex: $a= ganesh;

    $b=siva;Is_string(variable): it returns one when the variable stores the float value.

    Ex: $a=ganesh;Echo is_string($a);//it prints one.

    Compound data types:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    26

  • 8/8/2019 Php Nodes Full

    27/109

    [email protected] MySql, Php, Apache

    1) Array: An array is collection of elements. In php we can store different data typevalues in an array variable.

    Ex: $arr=array(10,20,34.5);Echo $arr[1];// it prints 20.

    Is_array(variable): it returns 1 when the variable stores the array values.Ex: echo is_array($arr);// it returns 1

    2) Object: object is an instance of class. Class is keyword to declare a classes in php.Syntax of object creation: $object variablename=new classnaem();

    Ex: Special data types :

    1) Resource data type: A resource variable re fers the external resource like databaseconnections .

    2) Null data type: In php null is not a value. Based on 2 conditions we can declare avariable as null data type.

    i) a variable is not assign with a value. Ex: $a; // it contains null valueii) a variable is assign with null. Ex: $a=null;

    is_null(): it returns one when the variable holds null value.Ex: $a=null;

    Echo is_null($a); // it prints 1Write a php script Example of testing the type of variable

    S.G.S Degree College, pippara T.Ganesh M.C.A

    27

  • 8/8/2019 Php Nodes Full

    28/109

    [email protected] MySql, Php, Apache

    -> Save the file in c:/server/apache2/htdocs/data.php . And open the browser gives the request toserver. i.e http://localhost/data.php .o/p : is null value?1is integer value?1is float value?1is string ?1is boolean ?1is array ?

    changing type with set type(): php provides settype() function, it is used to change the variable t ype. In this function we can passthe variable and changed data type and separated with comma.Syntax of set type:

    Settype(variablename,new type);Ex:

    o/p: is integer?1

    is float? 1

    is string?

    Opearators and expressions: operator is a symbol it is used to perform an action and produce a values. Php contains number of operators i.e

    Arithmetic Operators: arithmetical operator performs all mathematical calculations.

    Operator Name Example Result

    + Addition $x=2$x+2

    4

    - Subtraction $x=25-$x

    3

    * Multiplication $x=4$x*5

    20

    / Division 15/5 3

    % Modulus (division remainder) 5%210%2

    10

    ++ Increment x=5x++

    x=6

    S.G.S Degree College, pippara T.Ganesh M.C.A

    28

    http://localhost/data.phphttp://localhost/data.php
  • 8/8/2019 Php Nodes Full

    29/109

    [email protected] MySql, Php, Apache

    -- Decrement x=5x--

    x=4

    Assignment Operators: assignment operator is used to assign the values to variables andexpressions are assign to variables.

    Operator Nam e example

    = Assignment $X=10(10 is assign to x)$y=20(20 is assign to y)$x=($x+$y)

    Comparison Operators:

    comparision operator performs compare and test the given operands. It returns true or false values.

    Operator Description Example

    == is equal to $X=5,$y=10, $x==$y returns false

    != is not equal $x!=$y returns true

    > is greater than $x>$y returns false

    < is less than $X= is greater than or equal to $x>=$y returns false

  • 8/8/2019 Php Nodes Full

    30/109

    [email protected] MySql, Php, Apache

    Operator Nam e example

    ++ Increment $x=10;$x++;(it means increment the variable value byone) it returns 11

    -- Decrement $x=10;$x--;(decrease the variable value by one)//itcontains 9

    Concatenation operator:concatenation operator is represented by single period ( .)it used to combines more than one stringor statements.

    OperatorNam e example

    . Concatenation $a=hello;$b=php;Echo $a.$b;//it displays hello php

    Constants:Constants refers to fixed values it doesnt change the value at the time of execution of script.A variable can be defined constant, it doesnt change the variable value in entire script. In php, Avariable defines a constant by using define () built-in function.To use the define function , it place the name of the constant and gives the value within the

    parentheses and separated by commaSyntax:define(constant name,value);Ex:Write a php script Example of defining and accessing a constantTypes of Errors in PHP:

    .

    S.G.S Degree College, pippara T.Ganesh M.C.A

    30

  • 8/8/2019 Php Nodes Full

    31/109

    [email protected] MySql, Php, Apache

    Chapter-3. Flow Control Functi ons in PHP

    Control functions in php:

    Control functions are used to control the flow of execution. The control functions are two types inPhp. i.e switching flow and loops.Switching flow (or) conditional statements:

    the switching flow statements in php:

    if statement if...else statement (else clause with if statement) elseif....clause with the if statement switch statement

    if statement: the if statement is used to controlling the execution of statements. Using if statement to execute no of statements when ever the condition is true.

    Syntax:

    Write a program Example of if statement:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    31

    if(condition){

    //Statements;}

    output: the biggest no=20

  • 8/8/2019 Php Nodes Full

    32/109

    [email protected] MySql, Php, Apache

    if..else statement(else clause with if statement):if the condition is true then execute if block statements otherwise it executes else block statements. if block is called true block and else block is called false block.

    Syntax:

    Write a program Example of if..else state ment:

    Elseif clause with if statement: the series of blocks it executes only one block executeswhen the condition is true otherwise it executes else statements.

    Syntax:

    Write a program Example of elseif statement:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    32

    if(condition){

    //Statements;}Else{

    //statements;

    output: the biggest no=30

    if(condition1){

    //Statements1;}Elseif(condition2){

    //statements2}Else{

    //statements3}

  • 8/8/2019 Php Nodes Full

    33/109

    [email protected] MySql, Php, Apache

    The Switch statement: switch statement used to select one block of several blocks. It is aselection statement. It executes only one block of statements and it performs matching process.

    Syntax:

    In above syntax, first evaluate expression and it is compared to each case label. When thecase label is match that block of code is executed. break statement is used to exit the out of switch. Suppose there is no case label is match then executes default block.Ex:

    Ternary operator: (use ? operator):The ?: pair is a conditional operator (or) ternary operator.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    33

  • 8/8/2019 Php Nodes Full

    34/109

    [email protected] MySql, Php, Apache

    Syntax:Condition?expression1:expression2

    First evaluate condition, if condition is true then executes expression1 otherwise executesexpression2.Ex:

    Loops:Loops execute a block of code repeatedly until the condition i s falseThe loop statements are

    While statement

    Do..while For statement

    While statement: the while loop is a entry controlled loop. It executes a group of statements repeatedly until the condition is false.Syntax:

    In while loop, first execute the condition . here if the condition is true then thegroup of statements executes number of times until the condition is false.Ex:

    Do. while loop: : the do. while loop is a exit controlled loop. It executes a groupof statements repeatedly until the condition is false. But the condition is false thedo. while loop execute once.

    Syntax:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    34

    out ut: the bi est no=30

    While(condition){

    //statements}

    output: 1,2,3,4,10

    Do{

    //statements} While(condition);

  • 8/8/2019 Php Nodes Full

    35/109

    [email protected] MySql, Php, Apache

    Ex:

    For statement: for loop is a entry controlled loop. It exec utes a group of statements repeatedly until the condition is false.Synetx:

    Intilization part is used to declared the variables and assign the value to thevariable

    Condition part is used to test the condition. The condition is true then execute bodyof the loop.

    Inc/dec part is used to increment or decrement the variable.Ex:

    Nesting for loops: A for loop internally contains other for loops that is callednested for loop.Example of nested for loop : write a php script to print multiplication table 1 to 10in to a browser.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    35

    output: 1,2,3,4,10

    For (intilization; condition; inc/dec){

    //statements}

    output: 1 times 2 is 22 times 2 is 43 times 2 is 6.10 times 2 is 20

  • 8/8/2019 Php Nodes Full

    36/109

    [email protected] MySql, Php, Apache

    Break an d continue statements : break statement is used to break out of the loop. Continuestatement is used to continue the loop.

    Example of break statement:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    36

    Output:4000 divided by -4 is -10004000 divided by -3 is -1333.334000 divided by -2 is -20004000 divided by -1 is -4000

  • 8/8/2019 Php Nodes Full

    37/109

    [email protected] MySql, Php, Apache

    In above example solve the dividing a number zero and the execution of programdoes not stop using break statement.Example of continue statement:

    Chapter-4. Wor king with Functions in PHP

    Function:A function is self contained block of code that can be executed when ever called and performs a

    particular task.

    Functions are two types i.e built-in functions, user defined functions. Built- in functions arealready built in your php software when ever calling the function, the function code will beexecuted. User defined functions are defined by the user.Calling functions:

    A php contains hundreds of built-in functions. Each and every function executed by calling thename of function.Calling function syntax is:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    37

    Output:4000 divided by -4 is -10004000 divided by -3 is -1333.334000 divided by -2 is -20004000 divided by -1 is -40004000 divided by 1 is 40004000 divided by 2 is 2000.4000 divided by 10 is 400

  • 8/8/2019 Php Nodes Full

    38/109

    [email protected] MySql, Php, Apache

    The calling function provides name of the function and arguments. Suppose function can notcontain arguments then we calling without argument list.The abs() function is built-in function, and these function returns absulte value. For examplecalling the abs() function is like as below Strtoupper() is a predefined function, this is used to convert lower cas e letters to upper case letters.For example calling strupper() like as below. Defining function:Define the user defined function by using function statement. A function can define two types i.eFunction contain without arguments and function contain with arguments.Function contain with out argument:

    We define Function, it contains function name and arguments list and function body.Example of php script with define functiono/p: hello functionfunction contain arguments: A function define with arguments list that is called with argumentfunction.Syntax:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    38

    Function name($arguments list);

    function function_name(){

    //code}

    function function_name(arguments){

    //code}

  • 8/8/2019 Php Nodes Full

    39/109

    [email protected] MySql, Php, Apache

    Exampleo/p: hello ganeshreturning values from user-defined functions: function returns the values to calling function by using return statem ent. The return statementstops the execution of the function and value send back to calling codeexample of returning value:

    o/p: the result=11variable scope:A variable declared within a function that variable accessing only with in the function. It will not

    be executed outside the function or within other functions.For example creating a variable within a function and then prints the outside the function like as

    below script.

    Save the file i.e scope.php in server/apache2/htdocs folder.give the request to server i.ehttp://localhost/scope.php the result is like as below

    S.G.S Degree College, pippara T.Ganesh M.C.A

    39

    O/P: Notice error: undefined variable inc:\server\apache2\htdocs\scope.php on line 5This is

    http://localhost/scope.phphttp://localhost/scope.php
  • 8/8/2019 Php Nodes Full

    40/109

    [email protected] MySql, Php, Apache

    The value of $tvar is not printed because no variable exists outside the f1 function.Accessing variables with global statement:A variable declared within the php script that variable accessing all functions by using global

    statement. Suppose with out global statement the local variable is can not be accessing in other functions. Like as belowFor example variable defined outside the function are inaccessible from within function

    Save the file i.e test.php in server/apache2/htdocs folder.give the request to server i.ehttp://localhost/test.php the result is like as below

    Suppose you want to access an variable with in a function without passing an argument by usingglobal statement.The example of accessing global variables with global statement:

    Save the file i.e test2.php in server/apache2/htdocs folder.give the request to server i.ehttp://localhost/test2.php the result is like as below

    Saving state between function calls with the static statement:

    O/P: Notice error: undefined variable inc:\server\apache2\htdocs\scope.php on line 5The value=

    O/P: The value=200

    http://localhost/test.phphttp://localhost/test2.phphttp://localhost/test.phphttp://localhost/test2.php
  • 8/8/2019 Php Nodes Full

    41/109

    [email protected] MySql, Php, Apache

    $n++;echo $n."
    ";}f1();f1();f1();?>in above example the output is 10,10,10. here the n variable increment value is not stored betweenfunction calls. Using the static statement we have to store the n variable increment state betweenfunction calls like as below example. Save the file with static.php in c:/server/apache2/htdoc s. Now start the server and open the

    browser give the request to server i.e the url is http://localhost/static.phpo/p: 101112Passing variable references to functions:When u pass arguments to functions, they are stored as copies in parameter variable value. If anychanges in function variables then not changes to local variables. For example like as below

    Output:the function variable value=30the local variable value=20When u pass arguments to function by reference, they are stored as copies in parameter variable. If any changes in function variables then changes to local variables original value. For example likeas below

  • 8/8/2019 Php Nodes Full

    42/109

    [email protected] MySql, Php, Apache

    function f1(&$a){$a=$a+10;

    echo "the function variable value=".$a."
    ";}$x=20;f1($x);echo "the local variable value=".$x;?>Output:the function variable value=30the local variable value=30

    Chapter-5. Wor king with arrays in PHPArrays:Def: Array is a collection of elements with same type or different types.Syntax of array creation:

    $arrayname=array(elements);Creating array in php ny using array function. Array function is used to create the array with passing elements.

    Arrays in php has three types i.e Indexed arrays (or) numeric arrays Associative arrays Multidimensional arrays

    S.G.S Degree College, pippara T.Ganesh M.C.A

    42

  • 8/8/2019 Php Nodes Full

    43/109

    [email protected] MySql, Php, Apache

    Indexed arrays:numeric array stores each array element with a numeric index. In numeric array the index isautomatically assigned and the index will be start at 0.syntax:

    example of create indexed arrays:

    when u create array by using array function it stores the array elem ents like as below

    10 20 30 400 1 2 3

    //Write a php script to create array and display the array elements

    Print_r(): this function is used to display the array elements with keys.Syntax: print_r($arrayname);Without using for loop display the array elements i.e

    Associative arrays:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    43

    $arrayname=array(elements list);

    $a= array(10,20,30,40);

    Output: the array elements=10203040

    Output: the the array elements =Array ( [0] => 20 [1] =>30.5 [2] => ganesh [3] => 40 )

  • 8/8/2019 Php Nodes Full

    44/109

    [email protected] MySql, Php, Apache

    An associative array, each key is associated with a value. Or the array elements are stored innamed keys i.e the array is called associative array.

    syntex:

    example:

    when u create associative array . it stores the array elements like a s below

    10 20 30 40Nal kum jyo bhan

    Example of associative array

    multi dimensional arrays:

    An array is contains more than one array that is called multidimensional arrays.The multidimensional arrays holds more than one series of key/value pairs. create amultidimensional array, with automatically assigned ID keys

    Syntax: $variable name=array(array(elemnts),array(elemnts)..)Example:

    Example of php script:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    44

    $arrayname=array(name1=>value1,name=value2,..);

    $age=array(nal=>10,kum=>20,jyo=>30,bhan=>40);

    Output: the the array elements =Array ( [nal] => 10 [kum] => 20 [jyo] =>30 [bhan] => 40 )

    $student=array(array(name=>nali,age=>40,occupation=>cashier),array(name=>jyo,age=>50,occupation=>

    business));

    Output: the array elements =Array ( [0] => Array ( [name] => nali [age]=> 40 [occupation] => cashier ) [1] => Array ( [name] => jyo [age] => 50

    [occupation] => business ) ) jyo occupation=business

  • 8/8/2019 Php Nodes Full

    45/109

    [email protected] MySql, Php, Apache

    Array functions:There are number of array built-in functions in Php. i.e

    Count(): This function is used to count the array elements and i t returns the integer value.Syntax: count($array name);Ex: $a=array(10,20,30,40);

    Echo the array count=. Count($a);o/p: the array count=4Sizeof(): this funcation is used to find the given array size.

    Syntax: sizeof($array name);Ex: $a=array(10,20,30,40);Echo the array count=. sizeof($a);o/p: the array count=4

    Foreach(): The foreach statement is used to print the each element in an array.Syntax: foreach($array name as $value)

    {.

    }Ex: $a=array(10,20,30,ganesh);Echo the array elements=;Foreach($a as $value);{

    Echo $value;}

    o/p: the array elemnts=10,20,30, ganeshArray_push(): This fuction is used to insert the one or more elements to the end of anarray.

    Syntax: array_push($array name, element);

    Ex: $a=array(10,20,30,40);Array_push($a,50);Print_r($a);

    o/p: array[0=>[10] 1=>[20] 2=>[30] 3=>40]Array_pop(): This function is used to remove the last element of an array.

    Syntax: $remove element =array_pop($array name);Ex: $a=array(10,20,30,40);

    $r=Array_pop($a);

    S.G.S Degree College, pippara T.Ganesh M.C.A

    45

  • 8/8/2019 Php Nodes Full

    46/109

    [email protected] MySql, Php, Apache

    Echo the remove element =.$r;o/p:the remove element = 40

    Array_unshift: This function is used to Adds one or more elements to the beginning of anarray.

    Syntax: array_unshift($array name, element);Ex: $a=array(10,20,30,40);

    Array_unshift($a,50);Print_r($a);

    o/p: array[0=>[50] 1=>[10] 2=>[20] 3=>[30] 4=>[40]]Array_shift(): This function is used to remove the first ele ment of an array.

    Syntax: $remove first element =array_shift($array name);Ex: $a=array(10,20,30,40);

    $r=Array_pop($a);Echo the remove element =.$r;o/p:the remove element = 10

    Sort(): This function is used to sorting of given elements in array.(display ascending order)Syntax: $array variable =sort($array name);

    Ex: $a=array(20,50,40,10);$s=sort($a);Echo the sorting elements =;Print_r($s);

    o/p: the sorting elements =10,20,40,50rsort(): This function is used to sorting the elements in descending order of given array

    Syntax: $array variable =rsort($array name);Ex: $a=array(20,50,40,10);

    $s=sort($a);Echo the sorting elements =;Print_r($s);

    o/p: the sorting elements =5,40,20,10array_merge(): This function is used to combines the two given arrays.Syntax: $arrayvariable=array_merge($array1,$array2);Ex: $a=array(10,20,30);$b=array(40,50,60);$m=array_merge($a,$b);Print_r($m);o/p:10,20,30,40,50,60

    array_sum(): this function is used to find the sum of array elements and returns the sum of

    array. Syntax: $arrayvariable=array_sum($arrayname);Ex: $a=array(10,20,30);$b=array_sum($a);Echo the sum of array=.$b;o/p: 60

    array_product(): this function is used to find the product of given elements in an arrayand it returns the product value.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    46

  • 8/8/2019 Php Nodes Full

    47/109

    [email protected] MySql, Php, Apache

    Syntax: $arrayvariable=array_product($arrayname);Ex: $a=array(5,10,20,);$b=array_product($a);Echo the sum of array=.$b;o/p: 1000

    S.G.S Degree College, pippara T.Ganesh M.C.A

    47

  • 8/8/2019 Php Nodes Full

    48/109

    [email protected] MySql, Php, Apache

    UNIT-IIIWorking with objects an d forms

    Chapter-6. Working with Objects in PHP

    Creating Objects:

    Object is a run time entity which can hold class data i.e methods and class variables. Objects arealso called class variables.

    Syntax : $object variable=new classname();

    Class:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    48

  • 8/8/2019 Php Nodes Full

    49/109

    [email protected] MySql, Php, Apache

    A Class contains object properties(data) and object methods. These methods and data areaccessing with objects.Syntax:

    Class classname{

    //variables//object methods

    }

    Example:o/p: this is object method.

    Note: creation of method in a class by us ing function statement and this method is also calledobject method.

    Properties of objects:A variables are declared in a class these variables are also called properties of objects. Var keyword is used to declared the variables in a class.

    Class myclass{

    Var $a=10;Var $b=20; //properties of objects or variablesVar $c=30;

    }

    Example:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    49

    Output: a value=10 b value= 20 c value=30

  • 8/8/2019 Php Nodes Full

    50/109

    [email protected] MySql, Php, Apache

    Example: changing object properties

    Object methods:Methods add functionality to your objec ts. Methods are defined with in a class by usinf functionkeyword and A class methods are accessing with objects. The -> operator is used to call the objectmethods and variables.Example:o/p: hello php.Example: accessing class properties within a method

    S.G.S Degree College, pippara T.Ganesh M.C.A

    50

    Output: a value=50 b value= 100 c value=150

    Output: the addition value=30

  • 8/8/2019 Php Nodes Full

    51/109

    [email protected] MySql, Php, Apache

    Example: changing the value of a variable from within a method

    Const ructors:A constructor is a special method whenever class object is created then constructor willautomatically invoked. Constructor name and class name must be same.Constructors are two types i.eWithout arguments constructor Parameterized constructor

    Syntax:Class A{

    Function A() // with out argument constructor {

    .}

    }Example: without argument constrctor

    S.G.S Degree College, pippara T.Ganesh M.C.A

    51

    Output: the addition value=90

  • 8/8/2019 Php Nodes Full

    52/109

    [email protected] MySql, Php, Apache

    o/p: this is constructorExample: parameterized constructor o/p: the value=20

    Inheritance:Inheritance means get the object prope rties and object methods from one class to another class.the parent class data inherited to child class by using extends keyword.

    Inheritances are different types i.eSingle inheritanceMultiple inheritanceMultilevel inheritanceHierarchal inheritance

    S.G.S Degree College, pippara T.Ganesh M.C.A

    52

    Output: this is base classThis is child class

  • 8/8/2019 Php Nodes Full

    53/109

    [email protected] MySql, Php, Apache

    Chapter-7. Wor king with Strings, Dates, and Times

    Formatting Strings with PHP:You have simply printed any strings you want to display directly to the browser in their originalstate. Php provides two functions i.e printf() and sprintf(), that allow you first to apply formatting,i.e define alignment within a field and display the data to different number systems.working with printf():

    S.G.S Degree College, pippara T.Ganesh M.C.A

    53

  • 8/8/2019 Php Nodes Full

    54/109

    [email protected] MySql, Php, Apache

    printf() function is print the data directly on a browser with formatting. The printf() functionrequires a string argument that is known as format control string. It also accepts additionalarguments of different types.Syntax:

    Printf(control string,arg1,arg2,argn);Example:

    o/p: nalini wants 10 apples+

    the format control string is a special code known as a conversion s pecification. A conversionspecification begins with a percent symbol(%). It is used to covert the type specification andapplying formats to display the strings or data. The conversion specification is based on typespecifier.Type specifiers:Specifier description

    d display the decimal numbersb display the binary numbers.c display the characterss display the stringsf display the floating point numberso display the octal numbersExample:

    Field width specifier: The field width specifier is describe to allocate the width (spaces) of givenfield on a browser. The type specifier has proceeded by the number that indicates allocate the sizeof given field.

    For example:Printf (%4d,6);

    Field width specifier

    S.G.S Degree College, pippara T.Ganesh M.C.A

    54

    o/p: decimal: 543binary: 1000011111flaot: 543.00string: 543octal: 1037

  • 8/8/2019 Php Nodes Full

    55/109

    [email protected] MySql, Php, Apache

    Printf (%10f and %5d, 24, 30);Padding output with padding specifier: The padding specifier is used to padding the spaces of output field with 0 or space. The type specifier has proceeded by the zero or space that indicates

    padding specifier.For example:

    Printf(% 04d,62);

    Padding spacifier o/p:

    0 0 6 2working with printf():Sprintf() function is used to applying formatting of printed data. it is di rectly not printed the outputon a browser and it returns the output to another variable.Syntax:

    $return variable name=SPrintf(control string,arg1,arg2,argn);Example:

    o/p: nalini wants 10 apples

    String manipulations of PHP: (String functions in PHP)

    strlen(): The strlen() function is used to find the length of a given string.syntex:

    strlen(string);Example:

    Output: 12

    strstr(): This function is used to whether check the substring of given string. if substringis found it returns remaining string otherwise it returns false.

    syntex:strstr(string,substring);

    Example:

    Output: bhanu reka Example

    S.G.S Degree College, pippara T.Ganesh M.C.A

    55

  • 8/8/2019 Php Nodes Full

    56/109

    [email protected] MySql, Php, Apache

    Output: false[does not print any output]

    Strpos(): This function is used to returns the substring position of a given string. If a matchis found, it return the position of the first charcter. If no match is found, it will returnFALSE.

    Syntex:strpos(string,substring);

    Example:

    Output: 6

    Substr(): This function is used to display the substring of a given string.Syntex:

    substr(string,startindex, number of chars);Example:

    Output: reka

    Trim(): trim() is used to cut the first and last white spaces of given string.Syntex:

    trim(string);Example:

    Output: nalini

    Strtolower(): this function is used to the given string is converting to uppercase to lower case.

    Syntex:strtolower(string);

    Example:

    Output: lakshmi

    Strtoupper(): this function is used to the given string is converting to uppercase to lower case.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    56

  • 8/8/2019 Php Nodes Full

    57/109

    [email protected] MySql, Php, Apache

    Syntex:strtoupper(string);

    Example:

    Output: LAKSHMISubstr_replace(): this function is used to replace the substring of given string.

    Syntex:Substr_replace(string,substring, startindex, noofchars);

    Example:

    Output: ab25ef

    str_replace(): This function is used to replace all instances of a given string within another string.

    Syntex:str_replace(searchstr,replacestr, sourcestring);

    Example:

    Output: avis

    Explode(): this function is used to breaks up a string into an array. The explode() functionrequires two arguments i.e delimiter string and source string.

    Syntex:explode(delimiter, sourcestring);

    Example:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    57

  • 8/8/2019 Php Nodes Full

    58/109

    [email protected] MySql, Php, Apache

  • 8/8/2019 Php Nodes Full

    59/109

    [email protected] MySql, Php, Apache

    example:Getdate(): This function is used to get the current system date and it returns the array.

    Syntax: $a=getdate();Example:Output: 11-01-2010.

    Chapter 8: Working with Forms in PHP

    Creating a simple input form:Forms are used to provide the data or input on your program. a form contains no of input fields,these input fields is used to provide data to php code. Forms are created by using HTML language.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    59

  • 8/8/2019 Php Nodes Full

    60/109

    [email protected] MySql, Php, Apache

    tag is used to create a forms in HTML. The form tag contains two attributes i.e action andmethod.

    And forms are contains different types of input fields.The input tag is accepts different types field types .i.e.----------this tag is create a input tag. Input tag contains type attribute.

    Type=text--------it creates text boxType=submitit creates submit buttonType=password. It creates the password field.Type=checkbox.it creates the chekboxes.

    Method values are two types i.e. Get method and post method. The get method and post method isused to send the form data.Get: Appends the form-data to the URL and less amount of data is send. The data security is less.Post: it sends the body of the data and large amount of data is send. The data security is more.

    A simple html form is:

    Save the form.html in your webserver i.e c:\server\apache2\htdocs\form.html.Here The form is contains text filed with name txt1 and text area with the name message, and it iscontains send submit button.The form data accessing in php program here it is two global variables are used i.e. $_POST,$_GET$_POST: This variable is used to access the post method values.$_GET: This variable is used to access the get method values.

    Reading input from a form:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    60

    HTML FORM

    Name: Message:

  • 8/8/2019 Php Nodes Full

    61/109

    [email protected] MySql, Php, Apache

    save the form.php file in web server i.e. c:\server\apache2\htdocs\form.phpnow start the server and open the browser give the request to http://localhost/form.htmlnow it display the form below like thisinput form:

    The output is:

    Create a d ifferent types of input fields in HTML:Text Fields

    Text Fields defines a one-line input field that a user can enter text into:

    First name:
    Last name: the HTML code above looks in a browser:

    First name:Last name:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    61

    http://localhost/form.htmlhttp://localhost/form.htmlhttp://localhost/form.html
  • 8/8/2019 Php Nodes Full

    62/109

    [email protected] MySql, Php, Apache

    Password fileds:defines a password field:Password: the HTML code above looks in a browser:

    Password: (note password data is astrick symols)

    Radio Buttons

    defines a radio button. Radio buttons let a user select ONLY ONE of gi ven choices. Male
    Femalethe HTML code above looks in a browser:

    Checkboxes

    Checkboxes let a user select ONE or MORE options of a limited number of choices.

    Ex:

    I have a bike
    I have a car

    the HTML code above looks in a browser:

    Accessing form input with user-defined Arrays:

    The select element is used to choose one or more items from multiple select list. The user-definedarray means the multiple items insert on a one list here Adding multiple items in an array by usingselect tag and option tag. Option tag is used in a select tag.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    62

  • 8/8/2019 Php Nodes Full

    63/109

    [email protected] MySql, Php, Apache

    Syntex of select tag:

    Adding elements in list by using option tagSyntex og option tag:

    value1Example of accessing form input with user defined arrays:Creating html form:

    Save the form1.html in c:/server/apache2/htdo cs/ .Accessing form input data:

    Save form1.php in c:/server/apache2/htdocs. Now start the server and give the request to server i.ehttp://localhost/form1.html look like as below

    S.G.S Degree College, pippara T.Ganesh M.C.A

    63

    Name:
    Products
    :NokiaLGSonysamsung

    http://localhost/form1.htmlhttp://localhost/form1.html
  • 8/8/2019 Php Nodes Full

    64/109

    [email protected] MySql, Php, Apache

    Type name and select the products, when ever click the submit but ton it display the data on browser. Like as below figure.

    Combining HTML and php code on a single page:

    To combining a php code and HTML on to a single page follow the some procedure i.e.A file contains html tags and php tags now we have to save the file in php extension. combines a

    php code by using php standard tags.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    64

  • 8/8/2019 Php Nodes Full

    65/109

    [email protected] MySql, Php, Apache

    Save the file in web server i.e c:\server\apache2\htdocs\ m essage.php. after save start the server and give the request http://localhost/message.php and the result screen is like as below figure

    Give the name and password data in a text fileds and click the send button now display the resultis like as below

    Sending mail on form submission:

    The PHP mail() function is used to send emails froms.

    Syntax

    mail(to,subject,message,headers,parameters)

    S.G.S Degree College, pippara T.Ganesh M.C.A

    65

    Name: password:

    http://localhost/message.phphttp://localhost/message.phphttp://localhost/message.php
  • 8/8/2019 Php Nodes Full

    66/109

    [email protected] MySql, Php, Apache

    Parameter Description

    to Required. Specifies the receiver address

    subject Required. Specifies the subject of the email.

    message Required. Defines the message to be sent. Each line should be separated with anew line(\n). Lines should not exceed 70 characters

    headers Optional. Specifies additional headers, like From, Cc, and Bcc. parameters Optional. Specifies an additional parameters

    Example of creation of mail creation form in html:

    Save the mail.html in server. i.e c:\server\apache2\htdocs\maill.html .Creating script to send mail:

    Save the sendmail.php file in a server. i.e c:\server\apche2\htdocs\sendmail.php

    S.G.S Degree College, pippara T.Ganesh M.C.A

    66

  • 8/8/2019 Php Nodes Full

    67/109

    [email protected] MySql, Php, Apache

    Open the browser and Now give the request to server is http://localhost/mail.html . It display thelike as below form

    When e ver click the send button then the mail send to given receiver. Suppose if mail successfullysend it display the mail successfully send message on browser otherwise It displays mail sendingfailed like as below.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    67

    http://localhost/mail.htmlhttp://localhost/mail.htmlhttp://localhost/mail.htmlhttp://localhost/mail.html
  • 8/8/2019 Php Nodes Full

    68/109

    [email protected] MySql, Php, Apache

    Some Example scripts of forms in php://write a php script to create a form , a student select the numbar of subjects in givensubjects by using check boxes.

    Create a form in html:

    Accessing form input data:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    68

    Name
    SELECTED BSC SUBJECT
    mathematicsphysicscomputerscienceelectronicsstatastics

  • 8/8/2019 Php Nodes Full

    69/109

    [email protected] MySql, Php, Apache

    //write a php script to create a form , a student select the qualification option of given optionsby using option button.

    Crating form in html:

    Accessing form input:

    WORKING WITH FILE UPLOADS:File upload concept is used to upload the files to the server. By using the super global $_FILESarray you can upload files from a client to server.These fields are described in the below table: i.e.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    69

    Name
    select qualification
    INTERMEDIATEDEGREEMscMCA

    MBA

  • 8/8/2019 Php Nodes Full

    70/109

    [email protected] MySql, Php, Apache

    The first parameter is the form's input name and the second index can be either "name", "type","size", "tmp_name" or "error". Like this:

    $_FILES["file"]["name"] - the name of the uploaded file $_FILES["file"]["type"] - the type of the uploaded file $_FILES["file"]["size"] - the size of uploaded file $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the

    server $_FILES["file"]["error"] - the error code [note: file not uploade it provides >0 otherwise it

    provides

  • 8/8/2019 Php Nodes Full

    71/109

    [email protected] MySql, Php, Apache

    Here choose the file and upload the file and now it displ ays the output below like this:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    71

  • 8/8/2019 Php Nodes Full

    72/109

    [email protected] MySql, Php, Apache

    UN IT-IVIntroduction to Cookies, Working with

    Files and Images

    S.G.S Degree College, pippara T.Ganesh M.C.A

    72

  • 8/8/2019 Php Nodes Full

    73/109

    [email protected] MySql, Php, Apache

    Chapter 8: Working with Cookies and User Sessions

    Working with Cookies:

    Cookie:A cookie is used to identify a user. A cookie is a small file that the server stores on the user'scomputer. Each time the same computer requests a page with a browser, it will send the cookietoo. In php, you can create and retrieve the cookie values.

    operations on cookies is: Create a cookie Retrieve a cookie

    Delete a cookie

    Create a Cookie:The setcookie() function is used to create a cookies and it accepts five arguments.Syntaxsetcookie(name, value, expire, path, domain);

    In above sysntex name and value arguments is required and remaining attributes are optional.Example 1In the example below, we will create a cookie named "user" and assign the value "bhanu.vanumu"to it. We also

    Note: The value of the cookie is automatically URLencoded when sending the cookie, andautomatically decoded when received .Example 2You can also set the expiration time of the cookie. it specify that the cookie should expire after onehour:

    Note: In the example above the expiration time is set to a month ( 60 sec * 60 min * 24 hours * 30days ).Retrieve a Cookie Value:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    73

  • 8/8/2019 Php Nodes Full

    74/109

    [email protected] MySql, Php, Apache

    The PHP $_COOKIE variable is used to retrieve a cookie value.In the example below, we retrieve the value of the cookie named "user" and display it on a page:

    //Write a php script to find out the whether the cookie is set or not.In the following example we use the isset() function to find out if a cookie has been set or not:

    //to print all cookies on browser o/p: bhanu.vanumu

    panajavemavaramHow to Delete a Cookie?

    If u want to delete the cookie , then u will use the time() function with - symbol.Delete example:

    Working with sessions:

    A PHP session is used to store information on server. Session variables hold information about onesingle user, and are available to all pages in one application.

    Whenever the client sends the request to server , the server receives the request and it process therequest and result send back to the client. Here the same client again send the request to server ,the server does not identify same client send the another request because http protocol does notmaintain the state.

    A PHP session solves this problem by allowing you to store user information on the server.Sessions work by creating a unique id (UID) for each user.

    S.G.S Degree College, pippara T.Ganesh M.C.A

    74

  • 8/8/2019 Php Nodes Full

    75/109

    [email protected] MySql, Php, Apache

    Starting a PHP Session

    Before you can store user information in your PHP session, you must first start up the session.

    Note: The session_start() function used to start the session.

    Storing a Session Variable

    store and retrieve session variables is to use the PHP $_SESSION variable:

    Output:

    Pageviews=1

    // w.a php script to create a simple page view counter.

    we create a simple page-views counter. The isset() function checks session variable is set or not..

  • 8/8/2019 Php Nodes Full

    76/109

    [email protected] MySql, Php, Apache

    else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];?>

    Destroying a Session

    If you wish to delete some session data, you can use the unset() or the session_destroy() function.

    The unset() function is used to delete the session data:

    You can also completely destroy the session by calling the session_destroy() function:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    76

  • 8/8/2019 Php Nodes Full

    77/109

    [email protected] MySql, Php, Apache

    Chapter 9: Working with Files

    Including Files with include():

    The include() function is used to embedded or include the other files into your php program.The include() function is requires a single argument i.e file name.

    Note: suppose if your included file is stored in server then you not define to path otherwise definea /.Example of included file program:

    Save c:\server\apache2\htdocs\myinclude.php. Now above file is included in your program by using include(). Like as below example.

    Save:c:\server\apache2\htdocs\include.php. now open the browser and give the request to server i.e

    http://localhost//include.php

    Returning a value from an included document:Included files can return a values by using return statement and it returns the value when youinclude the file.Example://include another file

    Save in c:/server/apache2/htdocs/include2.php//an include file returns a value

    S.G.S Degree College, pippara T.Ganesh M.C.A

    77

    o/p: u r file is included

    http://localhost//include.phphttp://localhost//include.php
  • 8/8/2019 Php Nodes Full

    78/109

    [email protected] MySql, Php, Apache

    Using include() within control structures:Include() function is also using control structures i.e. if..else, for, while. In ifelse statement , if the condition is true then the file will be included otherwise not.Example

    Validating files:Php provides many functions to help to find out the inform ation about files on your system.The functions is

    File_exists() Is_file() Is_dir() Checking the status of a file

    1. is_readable()2. is_writable()3. is_executable()

    filesize()

    file_exists(): This function is used to find the whether the file is exists are not. The file_exists() is returns theBoolean value. Suppose the file exists it returns true.

    Synstex: File_exists(filename);Example:

    File or directory:Is_file() function is used to check whether it is a file or not. This function returns a Boolean

    values. Synstex:

    Is_file(filename)Example:

  • 8/8/2019 Php Nodes Full

    79/109

    [email protected] MySql, Php, Apache

    ?>

    Is_dir() is used to check whether it is directory or not. This function returns a Boolean values.

    Is_dir(directoryname)Example:

    is_readable(), is_writable(), is_executable():is_readble() function is used to check whether the file is readabl e or not. if it is readable file thenit returns true.Syntax:

    is_readable(filename)example:

    is_writable() function is used to check whether the file is writable or not. if it is writable file thenit returns true.Syntax:

    is_writable(filename)example:

    is_executable() function is used to check whether the file is executable or not. if it is executablefile then it returns true.Syntax:

    is_executable(filename)example:

    Filesize():This function is used to find the size of given file and it returns the size in the form of bytes.Syntax:

    filesize(filename)example:

    S.G.S Degree College, pippara T.Ganesh M.C.A

    79

  • 8/8/2019 Php Nodes Full

    80/109

    [email protected] MySql, Php, Apache

    Creating files and deleting files:In php, Creating files by using touch() function and deleting the files by using unlink() function.

    Touch(): This function is used to create the empty file in server.Syntax: touch(newfilename);Example:

    Unlink(): this function is used to delete the files.

    Syntax: unlink(filename);Example:

    File operations (or) opening a file for writin g, reading or appending:

    File operations: opening a file reading data from file writing data on a file appending data on a file closing a file

    opening a file: The fopen() function is used to open files in PHP.The first parameter of this function contains the name of the file to be opened and the second

    parameter specifies in which mode the file should be opened:

    The file may be opened in one of the following modes:Modes Description

    r Read only. Starts at the beginning of the file

    r+ Read/Write. Starts at the beginning of the file

    w Write only. Previous data is erased ,when new data is writing.

    a Append. Write continue the previous data file

    Syntex:$file=fopen("filename","mode");

    Example: // the file opens a read mode

    Example: // the file opens a write mode

    S.G.S Degree College, pippara T.Ganesh M.C.A

    80

  • 8/8/2019 Php Nodes Full

    81/109

    [email protected] MySql, Php, Apache

    Example: // the file opens a appand mode

    Reading data from files:Fgets(), fread(), fgetc() functions are used to reading data from files. Here reading the data fromfiles up to file end of the data by using feof(). The feof() is returns true when the file data is endedotherwise it returns false.Fgets(): This function is used to read the data from file with line by line

    Syntex: fgets(filepointer,bytes);Example:The example below reads a file line by line, until the end of file is reached: Fread(): This function is used to read the data from file with the number of bytes you want to read

    Syntex: fread(filepointer,bytes);Example:

    The example below reads a file data as number of bytes , until the end of file is reached:Fgetc: This function is used to read the data from character by character

    Syntex: fgetc(filepointer);Example:The example below reads a file line by line, until the end of file is reached:

  • 8/8/2019 Php Nodes Full

    82/109

    [email protected] MySql, Php, Apache

    {echo fgetc($fp). "
    ";}

    fclose($fp);?>Writing or appending to a file:

    the fwrite() function writes to an open file . when u have to use fwrite(), the previous file data iserased and write the new data. Suppose file is not exist it creates new file with data.Syntax:

    Fwrite(file pointer,data);

    the fputs() function writes to an open file . when u have to use fputs(), the previous file data is not

    erased and append the new data with existing data. Suppose file is not exist it creates new file withdata.Syntax:

    Fputs(file pointer,data);

    S.G.S Degree College, pippara T.Ganesh M.C.A

    82

  • 8/8/2019 Php Nodes Full

    83/109

    [email protected] MySql, Php, Apache

    Chapter 10: Working with imagesHow to create images in php:the basic php function is used to create a new image is called image create(), but creating image isnot a simple as just calling the function.Creating image is stepwise process and include different php functions. i.e. the process is

    Creating image area Drawing the shapes or images Fill the shapes Converting the image file type Destroy the image

    Drawing a new image:Creating an image begins with the image create() function, but In this function is set the canvasarea for new image. The following line creates a drawing area that is 150 pixels width and 150

    pixels height.$myimage=imagecreate(150,150);

    if u want to define a few colo