Unit 1
Unit 1
PHP started out as a small open source project that evolved as more and
more people found out how useful it was. Rasmus Lerdorf unleashed the
first version of PHP way back in 1994.
History of php
PHP was conceived sometime in the fall of 1994 by Rasmus Lerdorf. Early
non-released versions were used on his home page to keep track of who was
looking at his online resume. The first version used by others was available
sometime in early 1995 and was known as the Personal Home Page Tools. It
consisted of a very simplistic parser engine that only understood a few
special macros and a number of utilities that were in common use on home
pages back then. A guestbook, a counter and some other stuff. The parser
was rewritten in mid-1995 and named PHP/FI Version 2. The FI came from
another package Rasmus had written which interpreted html form data. He
combined the Personal Home Page tools scripts with the Form Interpreter
and added mSQL support and PHP/FI was born. PHP/FI grew at an amazing
pace and people started contributing code to it.
• It is difficult to give any hard statistics, but it is estimated that by late 1996
PHP/FI was in use on at least 15,000 web sites around the world. By mid-
1997 this number had grown to over 50,000. Mid-1997 also saw a change in
the development of PHP. It changed from being Rasmus' own pet project
that a handful of people had contributed to, to being a much more organized
team effort. The parser was rewritten from scratch by Zeev Suraski and
Andi Gutmans and this new parser formed the basis for PHP Version 3. A
lot of the utility code from PHP/FI was ported over to PHP3 and a lot of it
was completely rewritten.
• Today (end-1999) either PHP/FI or PHP3 ships with a number of
commercial products such as C2's StrongHold web server and RedHat
Linux. A conservative estimate based on an extrapolation from numbers
provided by NetCraft (see also Netcraft Web Server Survey) would be that
PHP is in use on over 1,000,000 sites around the world. To put that in
perspective, that is more sites than run Netscape's flagship Enterprise server
on the Internet.
PHP FEATURES
1. Simple and Easy to Learn
• PHP code runs on the server, generating HTML that is sent to the client’s
browser.
• Ideal for creating dynamic web pages.
3. Cross-Platform
• PHP has built-in libraries for tasks like sending emails, file handling,
encryption, image processing, and more.
7. Free and Open Source
Characteristics of PHP
1. Server-Side Language
• PHP runs on the server and generates dynamic content for websites.
• Example: It can create personalized web pages based on user input.
2. Open Source
• PHP works on all major operating systems like Windows, Linux, macOS,
etc.
• Code written in PHP can run anywhere, as long as the server supports it.
4. Embedded in HTML
• Web Server − PHP will work with virtually all Web Server software,
including Microsoft's Internet Information Server (IIS) but then most
often used is freely available Apache Server. Download Apache for free
here − https://httpd.apache.org/download.cgi
• Database − PHP will work with virtually all database software, including
Oracle and Sybase but most commonly used is freely available MySQL
database. Download MySQL for free here
− https://www.mysql.com/downloads/
phpinfo.php
<?php
phpinfo();
?>
The following section describe settings in httpd.conf that affect PHP directly
and cannot be set elsewhere. If you have standard installation then
httpd.conf will be found at /etc/httpd/conf:
The configuration file is well commented and thorough. Keys are case
sensitive, keyword values are not; whitespace, and lines beginning with
semicolons are ignored. Booleans can be represented by 1/0, Yes/No,
On/Off, or True/False. The default values in php.ini-dist will result in a
reasonable PHP installation that can be tweaked later.
Here we are explaining the important settings in php.ini which you may
need for your PHP Parser.
short_open_tag = Off
Short open tags look like this: <? ?>. This option must be set to Off if you
want to use XML functions.
safe_mode = Off
If this is set to On, you probably compiled PHP with the --enable-safe-
mode flag. Safe mode is most relevant to CGI use. See the explanation in
the section "CGI compile-time options". earlier in this chapter.
safe_mode_exec_dir = [DIR]
This option is relevant only if safe mode is on; it can also be set with the
--with-exec-dir flag during the Unix build process. PHP in safe mode only
executes external binaries out of this directory. The default is
/usr/local/bin. This has nothing to do with serving up a normal PHP/HTML
Web page.
safe_mode_allowed_env_vars =
[PHP_]
This option sets which environment variables users can change in safe
mode. The default is only those variables prepended with "PHP_". If this
directive is empty, most variables are alterable.
safe_mode_protected_env_vars =
[LD_LIBRARY_PATH]
This option sets which environment variables users can't change in safe
mode, even if safe_mode_allowed_env_vars is set permissively
disable_functions = [function1,
function2...]
A welcome addition to PHP4 configuration and one perpetuated in PHP5 is
the ability to disable selected functions for security reasons. Previously,
this necessitated hand-editing the C code from which PHP was made.
Filesystem, system, and network functions should probably be the first to
go because allowing the capability to write files and alter the system over
HTTP is never such a safe idea.
max_execution_time = 30
The function set_time_limit() won.t work in safe mode, so this is the main
way to make a script time out in safe mode. In Windows, you have to abort
based on maximum memory consumed rather than time. You can also use
the Apache timeout setting to timeout if you use Apache, but that will apply
to non-PHP files on the site too.
error_reporting = E_ALL &
~E_NOTICE
The default value is E_ALL & ~E_NOTICE, all errors except notices.
Development servers should be set to at least the default; only production
servers should even consider a lesser value
error_prepend_string = [""]
With its bookend, error_append_string, this setting allows you to make error
messages a different color than other text, or what have you.
warn_plus_overloading = Off
This setting issues a warning if the + operator is used with strings, as in a
form value.
variables_order = EGPCS
This configuration setting supersedes gpc_order. Both are now deprecated
along with register_globals. It sets the order of the different variables:
Environment, GET, POST, COOKIE, and SERVER (aka Built-in).You can change
this order around. Variables will be overwritten successively in left-to-right
order, with the rightmost one winning the hand every time. This means if you
left the default setting and happened to use the same name for an
environment variable, a POST variable, and a COOKIE variable, the COOKIE
variable would own that name at the end of the process. In real life, this
doesn't happen much.
register_globals = Off
This setting allows you to decide whether you wish to register EGPCS variables
as global. This is now deprecated, and as of PHP4.2, this flag is set to Off by
default. Use superglobal arrays instead. All the major code listings in this book
use superglobal arrays.
gpc_order = GPC
This setting has been GPC Deprecated.
magic_quotes_gpc = On
This setting escapes quotes in incoming GET/POST/COOKIE data. If you use
a lot of forms which possibly submit to themselves or other forms and display
form values, you may need to set this directive to On or prepare to use
addslashes() on string-type data.
magic_quotes_runtime = Off
This setting escapes quotes in incoming database and text strings. Remember
that SQL adds slashes to single quotes and apostrophes when storing strings
and does not strip them off when returning them. If this setting is Off, you
will need to use stripslashes() when outputting any type of string data from a
SQL database. If magic_quotes_sybase is set to On, this must be Off.
magic_quotes_sybase = Off
This setting escapes single quotes in incoming database and text strings with
Sybase-style single quotes rather than backslashes. If magic_quotes_runtime
is set to On, this must be Off.
auto-prepend-file = [path/to/file]
If a path is specified here, PHP must automatically include() it at the beginning
of every PHP file. Include path restrictions do apply.
auto-append-file = [path/to/file]
If a path is specified here, PHP must automatically include() it at the end of
every PHP file.unless you escape by using the exit() function. Include path
restrictions do apply.
include_path = [DIR]
If you set this value, you will only be allowed to include or require files from
these directories. The include directory is generally under your document
root; this is mandatory if you.re running in safe mode. Set this to . in order
to include files from the same directory your script is in. Multiple directories
are separated by colons: .:/usr/local/apache/htdocs:/usr/local/lib.
doc_root = [DIR]
If you.re using Apache, you.ve already set a document root for this server or
virtual host in httpd.conf. Set this value here if you.re using safe mode or if
you want to enable PHP only on a portion of your site (for example, only in
one subdirectory of your Web root).
file_uploads = [on/off]
Turn on this flag if you will upload files using PHP script.
upload_tmp_dir = [DIR]
Do not uncomment this line unless you understand the implications of HTTP
uploads!
session.save-handler = files
Except in rare circumstances, you will not want to change this setting. So
don't touch it.
ignore_user_abort = [On/Off]
This setting controls what happens if a site visitor clicks the browser.s Stop
button. The default is On, which means that the script continues to run to
completion or timeout. If the setting is changed to Off, the script will abort.
This setting only works in module mode, not CGI.
mysql.default_host = hostname
The default server host to use when connecting to the database server if no
other host is specified.
mysql.default_user = username
The default user name to use when connecting to the database server if no
other name is specified.
mysql.default_password = password
The default password to use when connecting to the database server if no
other password is specified.
(IIS) is by using the Microsoft® Web Platform Installer (Web PI). Web PI
completely automates setting up IIS, FastCGI, and the latest version of PHP
from the php.net Web site. With Web PI, you can navigate to the "Web
Platform" tab and select "PHP" under "Framework and Runtimes" customize
link. Alternately, use the instructions that follow as guidance for installing PHP
Open the Php.ini file in a text editor, then uncomment and modify settings as
follows:
• Set fastcgi.impersonate = 1.
FastCGI under IIS supports the ability to impersonate
security tokens of the calling client. This allows IIS to define
the security context that the request runs under.
• Set cgi.fix_pathinfo = 0
The cgi.fix_pathinfo provides PATH_INFO/PATH_TRANS
LATED support for Common Gateway Interface (CGI).
Setting this to 1 will cause PHP CGI to fix its paths to
conform to the specification.
• Set cgi.force_redirect = 0.
• Set open_basedir to point to a folder or network path
where the content of the Web site(s) is located.
• Set extension_dir to point to a location where PHP
extensions reside. For PHP 5.2.X, this is
typically extension_dir = "./ext".
• Set error_log="C:php_errors.log"
This can help with troubleshooting.
Here are the most important things to know about variables in PHP.
• All variables in PHP are denoted with a leading dollar sign ($).
• Variables are assigned with the = operator, with the variable on the left-
hand side and the expression to be evaluated on the right.
• Variables in PHP do not have built-in types - a variable does not know
in advance whether it will be used to store a number or a string of
characters.
The scope of a variable is the part of the script where the variable can be
referenced/used.
• local
• global
• static
Example:-
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
<?php
function myTest()
{
$x=5; //localscope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
To do this, use the global keyword before the variables (inside the
function):
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // output 15
?>
The PHP code example below shows a function which uses a static variable.
When the function is first called it won't have a value set so it's initialized with
the = 0 bit and then incremented on each subsequent call. Note that it doesn't
need to be an integer; any type should work just fine.
function foo() {
static $index = 0;
$index++;
echo "$index\n";
}
foo();
foo();
foo();
1
2
3
Variable Naming
Rules for naming a variable is −
Syntax
Parameter Description
<?php
$x = 25;
var_dump ($x);
echo "</br>";
$y = 32.5;
var_dump ($y);
echo "</br>";
$bvalue = true;
var_dump ($bvalue);
?>
Spaces are removed and replaced with the + character and any other non-
alphanumeric characters are replaced with a hexadecimal values. After the
information is encoded it is sent to the server.
• The GET method produces a long string that appears in your server logs,
in the browser's Location: box.
• GET can't be used to send binary data, like images or word documents,
to the server.
• The PHP provides $_GET associative array to access all the sent
information using GET method.
• The POST method does not have any restriction on data size to be sent.
• The POST method can be used to send ASCII as well as binary data.
• The data sent by POST method goes through HTTP header so security
depends on HTTP protocol. By using Secure HTTP you can make sure
that your information is secure.
• The PHP provides $_POST associative array to access all the sent
information using POST method.
PHP – Operator
What is Operator?
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
Arithmetic Operators
There are following arithmetic operators supported by PHP language −
Comparison Operators
There are following comparison operators supported by PHP language
Logical Operators
There are following logical operators supported by PHP language
Assignment Operators
There are following assignment operators supported by PHP language
Comments in PHP
A comment in PHP code is a line that is not executed as a part of the
program. Its only purpose is to be read by someone who is looking at the
code.
Example
/* This is a
multi-line comment */