Bca Final Unit 1 PHP
Bca Final Unit 1 PHP
Unit - I
PHP: Versions of PHP, Installation of PHP, Php.ini basics. Testing Installation. Building
Blocks of PHP: Variables, data types, Operators & Expressions, Constants, Switching, Flow,
Loops, Code Blocks and Browser Output.
PHP Intro:
PHP Features
PHP is very popular language because of its simplicity and open source. There are some
important features of PHP given below:
Version of PHP:
PHP is the widely used open source web development scripting/ programming language, that
can have HTML codes in its scripting. Since the first version release in 1994, there have been
5 more versions released for PHP upgradation purposes. The six versions of PHP are named
as „PHP version 1.0‟, „PHP version 2.0‟, „PHP version 3.0‟, „PHP version 4.0‟, „PHP version
5.0 and 6.0‟, and the current version „PHP version 7.0‟.
It originally used them to maintain his personal homepage. He later extended these code
specs to work with web forms and databases. It already had several basic functionalities. It
has form handling capabilities, Perl like variables and the ability to work with HTML.
One of the most interesting aspects included the way while loops were implemented. At the
end of the loop, the file pointer sought back to the saved position, and the whole loop was
reread and re-executed
core PHP and produced the Zend engine in. This new API made it possible to easily support
additional extensions for performing tasks such as accessing databases, spell checkers and
other technologies,
PHP 4 came with a new paradigm of "compile first, execute later." The compilation step does
not compile PHP scripts into machine code; it instead compiles them into byte code, which is
then executed by the Zend Engine (Zend stands for Zeev & Andi), the new heart of PHP 4.
It included several new features and better support for Object-Oriented Programming (OOP),
PDO (PHP Data Objects) extension which provides a lightweight interface to programmers
for accessing database and many other performance improvements. In the month of March
2010, PHP‟s version 6.0 was released with non-Unicode features like traits and closure
binding. This update left the developer community hoping for efficient Unicode integration.
During the period of 2016 and 2017, PHP underwent major changes and the update was
released as PHP 7. PHP 7 is based on phpng which stands for PHP next Generation. PHP
Next Generation was an initiative by developers to overcome the high-performance
constraints of previous PHP versions.
Install PHP :
To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack.
It is available for all operating systems. There are many AMP options available in the market
that are given below:
Step are :
Step 1: Click on the above link provided to download the XAMPP server according to your
window requirement.
Step 2: After downloading XAMPP, double click on the downloaded file and allow XAMPP
to make changes in your system. A window will pop-up, where you have to click on
the Next button.
Step 3: Here, select the components, which you want to install and click Next.
Step 4: Choose a folder where you want to install the XAMPP in your system and
click Next.
Step 6: XAMPP is ready to install, so click on the Next button and install the XAMPP.
Step 7: A finish window will display after successful installation. Click on the Finish button.
Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program
on the localhost.
PHP.ini :
PHP's initialization file, generally called php.ini, is responsible for configuring many of the
aspects of PHP's behavior. PHP comes packaged with two INI files. One that is
recommended to be used in production environments and one that is recommended to be
used in development environments. php.ini-production contains settings which hold security,
performance and best practices at its core
The php.ini file is a special file for PHP. It is where you declare changes to your PHP
settings. The server is already configured with standard settings for PHP, which your site will
use by default. Unless you need to change one or more settings, there is no need to create or
modify a php.ini file.
Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to
change the behavior of PHP at runtime, then this configuration file is to use.
All the settings related to register global variables, upload maximum size, display log errors,
resource limits, the maximum time to execute a PHP script and others are written in a file as a
set of directives which helps in declaring changes.
Testing PHP :
The easiest way to check the PHP configuration, including which modules are installed, is to
create a test script using the phpinfo() function.
<?php
phpinfo();
?>
All PHP code goes between the php tag. It starts with <?php and ends with ?>.PHP
statements ends with semicolon (;).
<html> Output :
<?php
?>
</body>
</html>
PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc.
Below some steps are given to run the PHP programs.
Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the
xampp folder.
Step 3: Run the XAMPP server and start the Apache and MySQL.
Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your
browser window.
Step 5: The output for the above hello.php program will be shown as the screenshot below:
Echo
PHP echo is a language construct, not a function. PHP echo statement can be used to print the
string, multi-line strings, escaping characters, variable, array, etc.
Variables
In PHP, a variable is declared using a $ sign followed by the variable name. Here, some
important points to know about variables:
o As PHP is a loosely typed language, so we do not need to declare the data types of the
variables. It automatically analyzes the values and makes conversions to its correct
datatype.
o After declaring a variable, it can be reused throughout the code.
o Assignment Operator (=) is used to assign the value to a variable.
o A variable must start with a dollar ($) sign, followed by the variable name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
o One thing to be kept in mind that the variable name cannot start with a number or special
symbols.
o PHP variables are case-sensitive, so $name and $NAME both are treated as different
variable.
<?php Output :
$x=5; $y=6; 11
$z=$x+$y;
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data
types that can be categorized further in 3 types:
It holds only single value. There are 4 scalar data types in PHP.
1. boolean
2. integer
3. float
4. string
It can hold multiple values. There are 2 compound data types in PHP.
1. array
2. object
1. resource 2. NULL
Operators
PHP Operator is a symbol i.e used to perform operations on operands. In simple words, operators
are used to perform operations on variables or values.
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
We can also categorize operators on behalf of operands. They can be categorized in 3 forms:
Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic operations such as
addition, subtraction, etc. with numeric values.
- Subtraction $a - $b Difference of
operands
* Multiplication $a * $b Product of
operands
/ Division $a / $b Quotient of
operands
% Modulus $a % $b Remainder of
operands
Assignment Operators
The assignment operators are used to assign value to different variables. The basic assignment operator is "=".
Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These operators
allow the evaluation and manipulation of specific bits within the integer.
& And $a & $b Bits that are 1 in both $a and $b are set to 1,
otherwise 0.
Comparison Operators
Comparison operators allow comparing two values, such as number or string. Below the list of
comparison operators are given:
!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and
they are not of same data type
<= Less than or equal to $a <= $b Return TRUE if $a is less than or equal $b
>= Greater than or equal to $a >= $b Return TRUE if $a is greater than or equal
$b
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease the value of a variable.
Logical Operators
The logical operators are used to perform bit-level operations on operands. These operators allow
the evaluation and manipulation of specific bits within the integer.
xor Xor $a xor $b Return TRUE if either $ or $b is true but not both
String Operators
The string operators are used to perform the operation on strings. There are two string operators in PHP, which
are given below:
Constants :
PHP constants are name or identifier that can't be changed during the execution of the script
except for magic constants, which are not really constants. PHP constants can be defined by 2
ways:
Constants are similar to the variable except once they defined, they can never be undefined or
changed. They remain constant across the entire program. PHP constants follow the same PHP
variable rules
Use the define() function to create a constant. It defines constant at run time.
<?php Output :
echo MESSAGE;
?>
Flow Control :
PHP if else statement is used to test condition. There are various ways to use if statement in
PHP.In PHP we have the following conditional statements:
If Statement
PHP if statement allows conditional execution of code. It is executed if condition is true.If
statement is used to executes the block of code exist inside the if statement only if the specified
condition is true.
<?php Output:
$num=5;
if($num<10) 5 is less than 100
{
echo "$num is less than 100";
}
?>
If-else Statement
PHP if-else statement is executed whether condition is true or false.
If-else statement is slightly different from if statement. It executes one block of code if the
specified condition is true and another block of code if the condition is false.
<?php Output:
$num=12;
if($num%2==0) 12 is even number
{
echo "$num is even number";
}
else
{
echo "$num is odd number";
}
?>
If-else-if Statement
The PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can
check multiple conditions using this statement.
80-100 – merit 60- 80 First 50- 60 Second 40- 50 Third <40 Fail
Nested if Statement
The nested if statement contains the if block inside another if block. The inner if statement
executes only when specified condition in outer if statement is true.
switch
PHP switch statement is used to execute one statement from multiple conditions. It works
like PHP if-else-if statement.
<?php Output :
$num=20;
switch($num){ number is equal to 20
case 10:
echo("number is equals to 10");
break;
case 20:
echo("number is equal to 20");
break;
case 30:
echo("number is equal to 30");
break;
default:
echo("number is not equal to 10, 20 or 30");
}
?>
Loops
Loops in PHP are used to execute the same block of code a specified number of times. PHP
supports following four loop types.
while − loops through a block of code if and as long as a specified condition is true.
do...while − loops through a block of code once, and then repeats the loop as long as a
special condition is true.
For loop :
PHP for loop can be used to traverse set of code for the specified number of times.
It should be used if the number of iterations is known otherwise use while loop. This means for
loop is used when you already know how many times you want to execute a block of code.
It allows users to put all the loop related statements in one place.
Parameters
The php for loop is similar to the java/C/C++ for loop. The parameters of for loop have the
following meanings:
initialization - Initialize the loop counter value. The initial value of the for loop is done only
once. This parameter is optional.
condition - Evaluate each iteration value. The loop continuously executes until the condition is
false. If TRUE, the loop execution continues, otherwise the execution of the loop ends.
<?php
for($n=1;$n<=10;$n++)
{
echo "$n<br/>";
}
?>
foreach loop
The foreach loop is used to traverse the array elements. It works only on array and object. It will
issue an error if you try to use it with the variables of different datatype.
The foreach loop works on elements basis rather than index. It provides an easiest way to iterate
the elements of an array.In foreach loop, we don't need to increment the value.
<?php Output:
//declare array
$name = array ("raj", "mohit", "Arun", "Rohit"); Raj
While Loop
PHP while loop can be used to traverse set of code like for loop. The while loop executes a block
of code repeatedly until the condition is FALSE. Once the condition gets FALSE, it exits from
the body of loop.It should be used if the number of iterations is not known.
The while loop is also called an Entry control loop because the condition is checked before
entering the loop body. This means that first the condition is checked. If the condition is true, the
block of code will be executed.
<?php Output:
$n=1;
while($n<=10) 12 3 4 5 6 7 8 9 10
{
echo "$n<br/>";
$n++;
}
?>
do-while loop
PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while
loop is guaranteed to run at least once.
The PHP do-while loop is used to execute a set of code of the program several times. If you have
to execute the loop at least once and the number of iterations is not even fixed, it is
recommended to use the do-while loop.It executes the code at least one time always because the
condition is checked after executing the code.
The do-while loop is very much similar to the while loop except the condition check. The main
difference between both loops is that while loop checks the condition at the beginning, whereas do-
while loop checks the condition at the end of the loop.
<?php Output :
$n=100;
Do 100
{
echo "$n<br/>";
$n++;
}
while($n<=10);
?>
The while loop is also named as entry control loop. The do-while loop is also named as exit
control loop.
The body of the loop does not execute if the condition The body of the loop executes at least
is false. once, even if the condition is false.
Condition checks first, and then block of statements Block of statements executes first and
executes. then condition checks.
This loop does not use a semicolon to terminate the Do-while loop use semicolon to terminate
loop. the loop.