--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
E-Mail : rupareleducation@gmail.com
Contact No: 7600044051
PHP Basic
Q-1: Explain PHP.
·
PHP is a server
side scripting language. that is used to develop Static websites or Dynamic
websites or Web applications. PHP stands for Hypertext Pre-processor, that
earlier stood for Personal Home Pages.
·
PHP scripts can
only be interpreted on a server that has PHP installed.
·
The client
computers accessing the PHP scripts require a web browser only.
·
PHP code may be
embedded into HTML code, or it can be used in combination with various web
template systems, web content management system and web frameworks.
·
Php Syntax
<?PHP
echo "Hello World";
?>
· According to
above syntax php has own starting and ending tag and use echo command to print
message and value on the screen.
·
Below are some of
the compelling reasons.
·
PHP is open
source and free.
·
Easy to learning compared to other languages such as
JSP, ASP etc.
·
Most web hosting
servers support PHP by default
·
PHP is regular
updated to keep abreast with the latest technology trends.
·
PHP is
a server side scripting language
·
PHP has in
built support for working in MySQL
Q-2: Explain PHP configuration in IIS
and Apache Web Server.
·
Configuration for
IIS
·
Open IIS manager,
select the host name of your computer in the Connections panel and then
double-click Handler Mapping.
·
In the Action
panel, click Add Module Mapping.
·
In Request path,
type *.php.
·
From Module menu,
select FastCgiModule.
·
In the Executable
box, type the full path to Php-cgi.exe, for example C:\PHP\Php-cgi.exe.
·
In Name, type a
name for the module mapping, for ex. FastCGI.
·
Click OK.
·
Select the host
name of your computer in the connection panel and double-click Default
Document.
·
In the Action
panel click Add. Type Index.php in the Name box and then Click OK.
·
Click Add again.
Type Default.php in the Name box, and then click OK.
Q-3: Configuration for Apache:
·
There are two
ways to setup Apache to use PHP. The first is to configure it to load the PHP
interpreter as an Apache module. The second is to configure it to run the
interpreter as a CGI binary. Choose the module method if your web host also
installed PHP as an Apache module and use the CGI method if they have
implemented it to run as a CGI(common gateway interface) binary.
·
To configure
Apache to load PHP as a module to parse your PHP scripts, use as ASCII text
editor to open the Apache configuration file, “httpd.conf”. If you use Apache
1.x, the file is found in “c:\Program Files\Apache Group\Apache\conf\”. Apache
2.0.x users can find it in “C:\Program Files\Apache Group\Apache2\conf\”.
·
Search for the
section of the file that has a series of “LoadModule” statements. Statements
prefixed by the “#” sign are regarded as having been commented out.
·
If you are using
Apache 1, you will have already placed your php.ini file in either the window
directory or somewhere in your PATH.
Q-4: Explain PHP configuration php.ini
(initialize) file.
·
Ans: The PHP
configuration file php.ini, is the final and most immediate way to affect PHP’s
functionality. The php.ini file is read each time PHP is initialized. In other
words, whenever httpd is restarted for the module version or with each script
execution for the CGI version.
·
The configuration
file is well commented and through. Keys are case sensitive, keyword values are
not; white space and lines beginning with semicolons are ignored.
·
The default
values in php.ini will result in a reasonable PHP installation that can be used
later. The important settings in php.ini which you need for your php parser.
·
Short_open_tag=off
·
Short open tags
look like thie <? ?>. This option must be set to Off if you want to use
XML functions.
Q-5: Explain PHP .htaccess file.
·
Ans: A .htaccess
(hyper text access) file is a directory level configuration file supported by
several web servers, that allows for decentralized management of web server
configuration. They are placed inside the web tree, and are able to override a
server’s global configuration for the directory that they are in and all sub
directories.
·
The original
purpose of .htaccess reflected in its name was to allow per-directory access
control, for example requiring a password to access the content.
·
To create a
.htaccess file on windows, just open a new document in Notepad and save it as
.htaccess and make sure All files are selected in the Save as type drop down
menu so it doesn’t save it as .htaccess.txt.
·
If you place an
.htaccess file in the root directory of your website, it will affect every
directory on your website.
Q.6: Benefit and Feature of PHP.
·
Cost:
o
PHP cost you
nothing, it is an open source software and does not need to purchase it for
development.
·
Easy to use:
o
PHP is easy to
learn, compared to others there are lots of readymade software are available
for PHP.
o
Scripts are
freely available in market.
o
So that you can
use them in your project or getting some help for them.
·
HTML support:
o
PHP is embedded
within HTML , in other words PHP pages are ordinary HTML pages. That escape
into PHP made only when necessary.
o
When a client
request for the web pages the web server pre process it.
o
This means it
goes through the page from top to bottom, looking for section of PHP which will
try to resolve.
·
Cross Platform Compatibility:
o
PHP and MYSQL
runs on every popular flavor of unix and windows. A huge percent of the world’s
HTTP server run on one of this two operating system.
·
PHP is compatible with three leading web services:
o
Apache HTTP
server for unix and windows, Microsoft internet information server(IIS), and
netscape enterprise server.
o
It also works
within several lesser known server, including Alex Blit’s FHttpd, Microsoft
personal web server, AOL server, Omnicentrix’s omni server.
·
Stability:
o
The word
stability means two different things in this content.
o
The server does
not need to be repeated.
o
The software does
not change from release to release.
·
Speed:
o
PHP is zippy in
its execution, especially when compiled as a Apache Module on the Unix side.
o
Although it takes
a slight performance hit. Rather than compiled this is the benefits of PHP
which drives from its status as a web server module.
Q-7: Explain PHP configuration in IIS and
Apache web server.
·
Start à control panel à Add/remove program
·
Click on
Add/remove window component and select IIS (Insert windows XP CD) in the CD ROM
drive.
·
Wait till IIS is
installed on your system.
·
To configure IIS
with PHP to start à control panel à Administration tools.
·
Expand the local
host window.
·
Right click on
default web sites and select properties.
·
In the website
properties window select the home directory tab and click on configure button.
·
In the
application configuration window click on add button.
·
In add/Edit
application extension dialog box browse the php.exe file and add the .php
extension in the text box and click on ok.
·
Restart the
machine in your Apache web server is configuring with php.
Q-8 What is Apache?
·
The Apache is
Http Server.
·
It is a powerful, flexible, HTTP/1.1
Compatible web server.
·
It implements the
latest protocol including HTTP/1.1.
·
It is highly
configurable and extensible with third party modules.
·
It can be
customized by writing modules using the Apache module API.
·
It provides full
source code and comes with unrestrictive license.
·
It runs on
windows NT/9x, Netware 5.x and above, OS/2 and most versions of UNIX, as well
as several other operating systems.
·
It encourages
user feedbacks through new ideas.
Q-9: Explain basic PHP syntax.
·
A PHP file
normally contains HTML tag just like HTML file with some PHP scripting code.
·
A PHP scripting
block starts with
<?php and ends with ?>
·
A PHP scripting
block can be placed anywhere in the HTML documents.
·
Each code line in
php must end with semicolon (;), the semicolon is a separator and is used to
separate one set of instructions from another.
·
There are two
basics statements for output with php is
·
echo
·
print
Ex.
<html>
<body>
<?php
echo
“Hello world”;
?>
</body>
</html>
Q-10: Explain characteristics of PHP.
·
PHP is case
sensitive.
o
PHP is most case
sensitive therefore have to take care about your capitalization closely when
you create or call variable object and functions.
·
Whitespace
o
PHP ignore extra spaces,
you can add white space to make your script more readable.
·
Insert special
character.
o
You can insert
special characters with a back slash.
o
Ex: echo “\” good
evening \” “;
·
Comments:
o
In PHP we use
forward slash // c style comments or # shell style comments, to make a single
line comment and /* and end with */ to make multi line comments.
·
Ending statement
with a semicolon
o
In php each
statement ends with a semicolon (;), it
is optional for last statement of php.
o
Ex:
<?php
echo
“Hello<br>”;
echo
“How are you<br>”;
echo
“I am fine<br>”
?>
Q-11: Explain variables in PHP.
·
Variables in PHP
are represented by dollar ($) sign followed by the name of the variable.
·
The variable name
is case sensitive. A valid variable name starts with a letter or underscore,
followed by any number of letters or underscores.
·
Ex:
<?php
$var=”Hello”;
$Var=”world”;
echo
“$var , $Var”;
?>
·
Output: Hello
world.
·
There are
different types of variable
·
Global variable:
·
In php global
variable must be declared global inside a function. If they are point to be
used that function using global keyword and php also defines $GLOBALS array.
·
Ex:
<?php
$a=1;
$b=2;
function
sum()
{
global
$a , $b;
$c=$a
+ $b;
}
sum();
echo
$c;
?>
Output: 3
·
Ex-2: Using GLOBALS keyword
<?php
$a=1;
$b=2;
function
sum()
{
$GLOBALS[‘c’]=$GLOBALS[‘a’]+$GLOBALS[‘b’];
}
sum();
echo
$c;
?>
Output=3
·
Scope of
variables:
·
The scope of a
variable contains within which it is defined.
·
Within user
define function a local function call scope is introduce any variable used
inside a function is by default to the limited for its local function scope.
Ex:
<?php
$a=1; /*global scope*/
function
test()
{
$a=100; /*local scope*/
echo
“Value of A=$a”;
/*reference
to local scope variable*/
}
test();
echo
“<br> value of A=$a”;
/*reference
to global scope variable*/
?>
·
Static variable:
·
It is another
important feature of variable scope.
·
A static variable
exist only local function scope, but it does not lose its value when program
execution leaves this scope.
Ex:
<?php
function
test()
{
$a=0;
echo
$a;
$a++;
}
test();
test();
test();
?>
·
This function is
quit useless because everytime when it is called it sets a=0 and print 0.
·
To make a useful
counting, the $a variable declared static.
Ex:
<?php
function
test()
{
static
$a=0;
echo
$a;
$a++;
}
test();
test();
test();
?>
Output:
0 1 2
Q-12: Explain GET and POST method.
·
When a form is
submitted to the php script, the information from that form is automatically
made available to the script.
·
There are many
way to access this information is to explain GET and POST method of form
object.
GET method:
·
The GET method
passes arguments from the page to the next page as a part of URL (Universal /
Uniform resource locator).
·
When used in form handling get append the indicated
variable name and value to the URL designated in the action attributes.
·
Each item
submitted via GET method is accessed in the handler via $_GET array.
·
GET sends its
variable in the URL of your visitors web browsers, which makes it easy to see
what we sent.
Advantages:
·
It construct and
actually new differentiable URL query string , so user can bookmark or store as
a favorite this page.
·
It also makes it
very easy for visitors to change what was sent,.
·
Disadvantages:
·
It is not
suitable for login form, because username and password is fully visible on the
screen.
·
Every get
submission is recorded in the web server.
·
The length of the
URL is limited, so that limited data pass using get method( The limitation of
data can be upto 255 characters).
POST Method
·
Post method is
most preferred method of form submission.
·
The form data set
is included in the body of the form. When it is forwarded to the processing
agent (web server).
·
No visible change
to the URL will result according to the different data submitted.
·
Each item
submitted via post method is accessed in the handler via the $_POST array.
Advantages
·
It is more secure
then GET because user enter information is never visible in the screen.
·
There is a much
large limit on the amount of the data that can be passed.(The limitation of
data is in a couple of kilobytes).
Disadvantages
·
The result at a
given moment can not be bookmarked.
·
The result should
be expire by the browser, so that an error will result if the user click the
back button to revisit the page.
·
This method can
be incompatible with certain firewall setup.
Note:
·
In programming of
GET and POST method you have to create two files, one file is HTML file and
another is PHP file.
·
When you run the
program, you have to run HTML file. In HTML file you have to give name of php
file with action attribute.
Q-13: Explain PHP operators.
·
There are three
types of operators.
·
1. Firstly there
is a unary operator which operates on only one value.
Ex: The negation operator (!) or increment
operator(++)
·
2. The second
group is termed as binary operator which contain most of the operator that php
supports.
Ex: Logical, conditional operator
·
3. The third
group is ternary operator (?:) it should be used to select between two
expressions depending on the third one.
Operators:
Arithmetic operators:
Example
|
Name
|
Result
|
$a + $b
|
Addition
|
Sum of $a and $b
|
!$a
|
Negation
|
Opposite of $a
|
$a - $b
|
Subtraction
|
Difference of $a and $b
|
$a * $b
|
Multiplication
|
Product of $a and $b
|
$a / $b
|
Division
|
Quotient of $a and $b
|
$a % $b
|
Modulus
|
Remainder of $a divided by $b
|
Note: The division operator return
float value anytime even if the two numbers are integer.
Increment Decrement operators
Example
|
Name
|
Result
|
++$a
|
Pre increment
|
Increment $a by one, then returns $a
|
$a++
|
Post Increment
|
Returns $a, then increment $a by one
|
--$a
|
Pre Decrement
|
decrement $a by one, then returns $a
|
$a--
|
Post decrement
|
Returns $a, then decrement $a by one
|
Assignment operator
Operator
|
Example
|
Is the
same as
|
=
|
$x=$y
|
$x=$y
|
+=
|
$x+=$y
|
$x=$x+$y
|
-=
|
$x-=$y
|
$x=$x-$y
|
*=
|
$x*=$y
|
$x=$x*$y
|
/=
|
$x/=$y
|
$x=$x/$y
|
%=
|
$x%=$y
|
$x=$x%$y
|
Comparison operator
Example
|
Name
|
Result
|
$a==$b
|
Equal
|
True if $a=$b
|
$a===$b
|
Identical
|
True if $a is equal to $b and they are of the same
type
|
$a!=$b
|
Not equal
|
True if $a is not equal to $b
|
$a <> $b
|
Not equal
|
True if $a is not equal $b
|
$a!==$b
|
Not identical
|
True if $a is not equal to $b, or they are not of
the same type.
|
$a < $b
|
Less then
|
True if $a is less then $b
|
$a > $b
|
greater then
|
True if $a is greater then $b
|
$a <= $b
|
Less then or equal to
|
True if $a is less then or equal to $b
|
$a >= $b
|
Greater then or equal to
|
True if $a is greater then or equal to $b
|
Logical operator
Example
|
Name
|
Result
|
$a and $b
|
And
|
True if both $a and $b are true
|
$a or $b
|
Or
|
True if
either $a or $b is true
|
$a XOR $b
|
XOR
|
True if either $a or $b is true but not both
|
!$a
|
NOT
|
True if $a is not true
|
$a && $b
|
And
|
True if both $a and $b are true
|
$a || $b
|
Or
|
True if
either $a or $b is true
|
Ternary Operator:
·
One special
useful operator is ternary conditional operator.
·
Its job is to
takes three expression and use truth value of the expression to decide which of
the other two expressions to evaluate and return.
Syntax:
test expression ? yes-expression : no expression
·
The value of this
expression is the result of yes-expression if test-expression is true;
otherwise no-expression.
String operators:
·
There are two
string operators.
·
The first is the
concatenation operator (‘.’), which returns the concatenation of its right and
left arguments.
·
The second is the
concatenating assignment operator(‘.=’),which appends the argument on the right
side to the argument on the left side.
Ex:
<?php
$a=”Hello”;
$b=$a.”world!”; //now
$b contains “Hello world!”
$a=”Hello”;
$a.=”world”; //now
$a contains “Hello world”
?>
Q-14: Explain Conditional structure and
looping structure.
Or
Explain Control structure and looping
structure.
If-
statement:
·
The if construct
is one of the most important features of many languages, php included. It
allows for conditional execution of code fragments.
·
PHP features an
if structure is similar to that of C:
if (expression)
{
Statements
}
·
As described in
the section about expressions, expression is evaluated to its Boolean value.
·
If expression
evaluates to TRUE, PHP will execute statements, and if it evaluates to FALSE –
it’ll ignore it.
Ex:
<?php
if($a>$b)
{
echo
“a is bigger than b”;
}
?>
if…else:
·
often you want to
execute a statement if a certain condition is met, and a different statement if
the condition is not met. This is what else is for.
·
Else extends an
if statement to execute a statement in case the expression in the if statement
evaluates to FALSE.
if(expression)
{
Block-1
}
else
{
Block-2
}
·
If expression
evaluates to TRUE then block-1 is executed else block-2 is executed.
·
Ex:
<?php
If($a
> $b)
{
echo
“a is bigger than b”;
}
else
{
echo
“a is not bigger than b”;
}
?>
·
if….else if….else
·
You should use
the if…else if…else statement if you want to select one of many sets of lines
to execute.
Syntax:
if(condition1)
{
Code to
be executed if condition1 is true
}
else if(condition2)
{
Code to
be executed if condition2 is true
}
else
{
Code to be executed if condition1 and
condition2 are not true
}
Switch
Statement
·
You should use
the switch statement if you want to select one of many blocks of code to be
executed.
·
First we have a
single expression (n) that is evaluated once.
·
The value of the
expression is then compared with the values for each case in the structure. If
there is a match, the block of code associated with that case is executed.
·
Use break to
prevent the code from running into the next case automatically.
·
If expression is
not match with any case then default block is executed.
·
Syntax:
switch(n)
{
case 1:
execute
code block 1
break
case 2:
execute
code block 2
break
default:
code
to be executed if n is different from case 1 and 2
}
Looping
Structure:
·
Very often when
you write code, you want the same block of code to run over again in a row.
Instead of adding several almost equal lines in a script we can use loops to
perform a task like this.
·
In php there is
two different kind of loops.
·
For-loops
through a block of code a specified number of times
·
While- loops
through a block of code while a specified condition is true.
While:
·
While loops are
the simplest type of loop in PHP. They behave just like their C counterparts.
The basic form of a while statement is
while(expr)
{
Statements
}
·
The meaning of a
while statement is simple. It tells PHP to execute the nested statements
repeatedly, as long as the while expression evaluates to true.
·
The value of the
expression is checked each time at the beginning of the loop.
·
So even if this
value changes during the execution of the nested statement, execution will not
stop until the end of the iteration.
·
Sometimes if the
while expression evaluates to false from the beginning.
do…while
·
do-while loops
are very similar to while loops, except the truth expression is checked at the
end of each iteration instead of in the beginning.
·
The main
difference from regular while is that the first iteration of a do…while loop is
guaranteed to run.
The basic formation of the do…while statement is,
do
{
Statements
}while(expr);
For:
For loops are the most complex loops in PHP.
They behave like their C counterparts.
The syntax of a for loop is
For(expr1;expr2;expr3)
{
Statements;
}
·
The first
expression is evaluated once unconditionally at the beginning of the loop.
·
In the beginning
of each iteration expression-2 is evaluated. If it evaluates to true, the loop
continuous and the nested statement executed.
·
If it evaluates
to false, the execution of the loop ends.
·
At the end of
each iteration expression-3 is evaluated.
Foreach
loop:
·
The foreach loop
works only on arrays, and is used to loop through each key/value pair in an
array.
Syntax
foreach ($array as $value)
{
code to be executed;
}
code to be executed;
}
·
For every loop
iteration, the value of the current array element is assigned to $value and the
array pointer is moved by one, until it reaches the last array element.
Q-15 :Explain Array In php.
·
An array in Php
is actually an ordered map.
·
A map is a type
that maps values to keys.
·
This type is
optimized in several ways, so you can use in as a real array or a list,
dictionary, collection, queue and more.
·
The array is
specified with array () function.
·
In PHP, the
array() function is used to create an array:
·
It takes a
certain number or comma separated by
·
key => values
pair.
Syntax:
Array([key=>]value,…..)
·
A key may be an
integer or a string. If a key is a standard representation of an integer, it
will be interpreted as its value.
·
Floats in key are
truncated to integer.
·
There is only one
type which can both contain integer and string.
·
A value can be of
any PHP type.
·
A key may be
integer or string and value may be any value.
Ex:
<?php
$arr=array(“hello”=>
“students”,12=> “true”);
echo
$arr[“hello”];
echo
$arr[12];
?>
·
It will be
interpreted as integer. There are no different index and according type in PHP.
For Ex:
<?php
$arr=array(“somearray”=>array(6=>5,14=>9,”a”=>42));
echo $arr[“somearray”][6];
echo $arr[“somearray”][14];
echo $arr[“somearray”][“a”];
?>
·
If you do not specify
a key for a given value, that maximum of the integer value is taken and new key
will be that maximum value +1.
·
If you specify a
key that already has a value assign to it, than value will be overwritten.
Ex:
<?php
Array(5=>43,32,8=>56,”b”=>12)
?>
·
Using true as a
key will evaluate to integer 1 as key.
·
Using false as a
key will evaluate to integer 0 as key.
·
You can modify an
existing array by setting the value in array.
·
It is done by the
assigning to array while specify the key in brackets. You can also omit the key
and add empty pair of brackets([ ]) to the variable name.
·
If variable does
not exist yet it will be created , so that this is also alternative way to
specify an array.
·
To change a
certain value, just assign a new value with its key.
·
If you want to
remove a key and value of a pair, you need to unset it.
Ex:
<?php
$arr=array(5=>1,12=>2);
$arr[ ]=56;
$arr[“x”]=42;
print_r($arr);
unset($arr[5]);
echo “<br>”;
print_r($arr);
?>
According to above example unset is used to remove elements
in an array.
:: Best Of Luck ::
Please give your comment and share it... Thank you.