0% found this document useful (0 votes)
113 views

Bca Final Unit 1 PHP

The document provides an overview of PHP including its history, versions, features, installation and configuration. It discusses how to install PHP with XAMPP, edit the php.ini file and test PHP installations. The document is intended as educational material on PHP fundamentals.

Uploaded by

piyushswami2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Bca Final Unit 1 PHP

The document provides an overview of PHP including its history, versions, features, installation and configuration. It discusses how to install PHP with XAMPP, edit the php.ini file and test PHP installations. The document is intended as educational material on PHP fundamentals.

Uploaded by

piyushswami2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

BCA FINAL PHP WEBSOL

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 is an acronym for "PHP: Hypertext Preprocessor"


 PHP is a widely-used, open source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use
 PHP files can contain text, HTML, CSS, JavaScript, and PHP code
 PHP code is executed on the server, and the result is returned to the browser as plain
HTML
 PHP files have extension ".php"
 PHP can generate dynamic page content
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 PHP supports a wide range of databases
 PHP is a server scripting language, and a powerful tool for making dynamic and
interactive Web pages.
 PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's
ASP.
 PHP was created by Rasmus Lerdorf in 1994 but appeared in the market in 1995
 PHP Syntax is C-Like
 PHP 7.4.0 is the latest version of PHP
 PHP is an object-oriented language.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 1


BCA FINAL PHP WEBSOL

PHP Features

PHP is very popular language because of its simplicity and open source. There are some
important features of PHP given below:

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 2


BCA FINAL PHP WEBSOL

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‟.

PHP Version 1.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.

PHP Version 2.0

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

PHP Version 3.0

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 Version 4.0

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.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 3


BCA FINAL PHP WEBSOL

PHP Version 5.0 and 6.0

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.

Current Version 7.0

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:

o WAMP for Windows


o LAMP for Linux
o MAMP for Mac
o SAMP for Solaris
o FAMP for FreeBSD
o XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other
components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 4


BCA FINAL PHP WEBSOL

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.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 5


BCA FINAL PHP WEBSOL

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.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 6


BCA FINAL PHP WEBSOL

Step 5: Click Next and move ahead.

Step 6: XAMPP is ready to install, so click on the Next button and install the XAMPP.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 7


BCA FINAL PHP WEBSOL

Step 7: A finish window will display after successful installation. Click on the Finish button.

Step 8: Choose your preferred language.

Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program
on the localhost.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 8


BCA FINAL PHP WEBSOL

How to run PHP programs on XAMPP

Step 10: If no error is shown, then XAMPP is running successfully.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 9


BCA FINAL PHP WEBSOL

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.

Important settings or common parameters of the php.ini file:


1. enable_safe_mode = on
2. register_globals = on
3. upload_max_filesize
4. upload_tmp_dir = [DIR]
5. post_max_size
6. display_errors = off
7. error_reporting = E_ALL & ~E_NOTICE: .
8. error_prepend_string = [“”] max_execution_time = 30
9. short_open_tags = Off
10. include_path = [DIR]

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 10


BCA FINAL PHP WEBSOL

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();
?>

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 11


BCA FINAL PHP WEBSOL

How to run PHP code in XAMPP


Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to
create a simple PHP example. To do so, create a file and write HTML tags + PHP code and
save this file with .php extension.

All PHP code goes between the php tag. It starts with <?php and ends with ?>.PHP
statements ends with semicolon (;).

<html> Output :

<body> Hello First PHP

<?php

echo "<h2>Hello First PHP</h2>";

?>

</body>

</html>

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 12


BCA FINAL PHP WEBSOL

How to run PHP programs in XAMPP


How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP
programs can be written on any editor, such as - Notepad, Notepad++, Dreamweaver, etc. These
programs save with .php extension, i.e., filename.php inside the htdocs folder.

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 1: Create a simple PHP program like hello world.

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.

o echo is a statement, which is used to display the output.


o echo can be used with or without parentheses: echo(), and echo.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 13


BCA FINAL PHP WEBSOL

o echo does not return any value.


o We can pass multiple strings separated by a comma (,) in echo.
o echo is faster than the print statement.

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.

Rules for declaring PHP 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;

echo $z; ?>

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 14


BCA FINAL PHP WEBSOL

PHP Data type:

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:

PHP Data Types: Scalar Types

It holds only single value. There are 4 scalar data types in PHP.

1. boolean
2. integer
3. float
4. string

PHP Data Types: Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

1. array
2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.

1. resource 2. NULL

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 15


BCA FINAL PHP WEBSOL

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.

PHP divides the operators in the following groups:

 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:

o Unary Operators: works on single operands such as ++, -- etc.


o Binary Operators: works on two operands such as binary +, -, *, / etc.
o Ternary Operators: works on three operands such as "?:".

Arithmetic Operators

The PHP arithmetic operators are used to perform common arithmetic operations such as
addition, subtraction, etc. with numeric values.

Operator Name Example Explanation

+ Addition $a + $b Sum of operands

- Subtraction $a - $b Difference of
operands

* Multiplication $a * $b Product of
operands

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 16


BCA FINAL PHP WEBSOL

/ 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 "=".

Operator Name Example Explanation

= Assign $a = $b The value of right operand is assigned to the left operand.

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.

Operator Name Example Explanation

& And $a & $b Bits that are 1 in both $a and $b are set to 1,
otherwise 0.

| Or (Inclusive or) $a | $b Bits that are 1 in either $a or $b are set to 1

^ Xor (Exclusive or) $a ^ $b Bits that are 1 in either $a or $b are set to 0.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 17


BCA FINAL PHP WEBSOL

Comparison Operators
Comparison operators allow comparing two values, such as number or string. Below the list of
comparison operators are given:

Operator Name Example Explanation

== Equal $a == $b Return TRUE if $a is equal to $b

=== Identical $a === $b Return TRUE if $a is equal to $b, and they


are of same data type

!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and
they are not of same data type

!= Not equal $a != $b Return TRUE if $a is not equal to $b

<> Not equal $a <> $b Return TRUE if $a is not equal to $b

< Less than $a < $b Return TRUE if $a is less than $b

> Greater than $a > $b Return TRUE if $a is greater than $b

<= 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.

Operator Name Example Explanation

++ Increment ++$a Increment the value of $a by one, then return $a

$a++ Return $a, then increment the value of $a by one

-- decrement --$a Decrement the value of $a by one, then return $a

$a-- Return $a, then decrement the value of $a by one

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 18


BCA FINAL PHP WEBSOL

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.

Operator Name Example Explanation

and And $a and $b Return TRUE if both $a and $b are true

Or Or $a or $b Return TRUE if either $a or $b is true

xor Xor $a xor $b Return TRUE if either $ or $b is true but not both

! Not ! $a Return TRUE if $a is not true

&& And $a && $b Return TRUE if either $a and $b are true

|| Or $a || $b Return TRUE if either $a or $b is true

String Operators
The string operators are used to perform the operation on strings. There are two string operators in PHP, which
are given below:

Operator Name Example Explanation

. Concatenation $a . $b Concatenate both $a and $b

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 19


BCA FINAL PHP WEBSOL

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:

1. Using define() function


2. Using const keyword

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

PHP constant: define()

Use the define() function to create a constant. It defines constant at run time.

<?php Output :

define("MESSAGE","Hello PHP"); Hello PHP

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 - executes some code if one condition is true


 if...else statement - executes some code if a condition is true and another code if that
condition is false
 if...elseif...else statement - executes different codes for more than two conditions
 switch statement - selects one of many blocks of code to be executed

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 20


BCA FINAL PHP WEBSOL

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.

Note : WAP student marksheet with various condition:

80-100 – merit 60- 80 First 50- 60 Second 40- 50 Third <40 Fail

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 21


BCA FINAL PHP WEBSOL

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.

Note : WAP Max between three number

switch
PHP switch statement is used to execute one statement from multiple conditions. It works
like PHP if-else-if statement.

about switch case:


1. The default is an optional statement. Even it is not important, that default must always be
the last statement.
2. There can be only one default in a switch statement. More than one default may lead to
a Fatal error.
3. Each case can have a break statement, which is used to terminate the sequence of
statement.
4. The break statement is optional to use in switch. If break is not used, all the statements
will execute after finding matched case value.
5. PHP allows you to use number, character, string, as well as functions in switch
expression.
6. Nesting of switch statements is allowed, but it makes the program more complex and less
readable.
7. You can use semicolon (;) instead of colon (:). It will not generate any error.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 22


BCA FINAL PHP WEBSOL

<?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.

 for − loops through a block of code a specified number of times.

 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.

 foreach − loops through a block of code for each element in an array.

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.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 23


BCA FINAL PHP WEBSOL

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.

Increment/decrement - It increments or decrements the value of the variable.

<?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

//access array elements using foreach loop Mohit


foreach ($name as $element) {
echo "$element"; Arun
echo "</br>";
} Rohit
?>

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 24


BCA FINAL PHP WEBSOL

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.

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 25


BCA FINAL PHP WEBSOL

<?php Output :
$n=100;
Do 100
{
echo "$n<br/>";
$n++;
}
while($n<=10);
?>

Difference between while and do-while loop

while Loop do-while loop

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.

------------ BEST OF LUCK-----

WEBSOL TECHNOSYS DR. AMIT VYAS SIR 9214525215 Page 26

You might also like