PHP Unit-1
PHP Unit-1
. Earlier customers are very happy with products to purchase from this
website.
But with more traffic and user base.
Rob Started receiving more complaint to the customer,not related to the
products,but website itself
PHP Useful in many ways:
Syntax
<?php
PHP code goes here
?>
Example:
<html>
<head>
<title>PHP Hello World</title>
</head>
<body>
<?php echo "Hello, World! This is PHP code";
?>
</body>
</html>
Output:
Hello, World! This is PHP code
Features of PHP
● Simple:`Easy to learn language
● Highly flexible:changes can be done without developers
intervention
● Platform Independent: It Runs on every platform.
● Interpreted: It does not need to be complied
● Fast:It loads websites faster
● Open Source:it is free to use and implemented
● Secure:The Php frameworks provide security to websites
● Large PHP Community:since php is a old language it as
grown up to a large community which is very helpful and makes
a language is most stable
PHP Case Sensitivity: In PHP, keywords (e.g. if, else, while, echo,
etc.), classes, functions, and user-defined functions are not
case-sensitive.
<!DOCTYPE html>
<html> -
<body>
<?php
</body>
</html>
Output:
Installation and Configuration of php:
Install PHP:
To install PHP, we will suggest you install AMP(Accelerated Mobile Pages) (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:
○ XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes
some other components too such as FileZilla, OpenSSL, Webalizer, Mercury
Mail, etc.
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 5: Click Next and move ahead.
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 8: XAMPP is ready to use. Start the Apache server and MySQL and run the php
program on the localhost.
Embedding PHP code in your webpage:
Embedding PHP code into a web page allows you to dynamically generate content
based on server-side logic. Here's a basic example of embedding PHP code within an
HTML file
● <?php and ?> are PHP opening and closing tags, respectively.
● Inside the PHP tags, you can write PHP code.
● echo is a PHP function used to output content.
● date("Y-m-d") is a PHP function that returns the current date in the specified
format.
● Variables in PHP start with the dollar sign $.
● You can mix HTML and PHP freely within a PHP file.
When a user visits this web page, the PHP code will be executed on the server-side,
and the resulting HTML (along with any output generated by PHP) will be sent to the
user's browser.
<html>
<body>
<h1> First PHP Webpage </h1>
<?php
?>
</body>
</html>
In this example:
Understanding PHP:
PHP (Hypertext Preprocessor) is a server-side scripting language that is widely used
for web development. It's particularly well-suited for creating dynamic web pages
and
interacting with databases. Here are some key concepts to understand about PHP:
Indexes start with 0 and are Keys are assigned manually and can be
automatically assigned strings too
Example: Example:
$empdetails[“Sam”] = “1200”;
form after submitting it. When form uses method post to transfer data,
the data is not visible in the query string, because of which security levels
HTML form after submitting it. When form uses method get to transfer
data, the data is visible in the query string, therefore the values are not
hidden. $_GET super global array variable stores the values that come in
the URL.
In HTML, white space refers to spaces, tabs, and line breaks. White space is
generally ignored by web browsers when rendering HTML content. This means that
you can use white space in your HTML code to format and organize it in a way that
makes it more readable for humans without affecting the layout of the rendered
page.
Here are a few key points to understand about white space in HTML:
1. Spaces and Tabs: Whether you use spaces or tabs for indentation in your
HTML code is a matter of personal preference. Browsers will treat both
spaces and tabs as white space and ignore them when rendering the page.
2. Line Breaks: Line breaks in the HTML code are also considered white space
and are generally ignored by browsers. This means that you can use line
breaks to break up long lines of code or to separate different sections of your
HTML document without affecting the layout of the rendered page.
In PHP, you can write comments to document your code or temporarily disable
certain sections of code. There are two types of comments in PHP: single-line
comments and multi-line comments.
● Multi-Line Comments: Multi-line comments start with /* and end with */.
They can span multiple lines and are useful for adding comments that span
several lines.
<?php
echo "Hello, world!";
/* This is a multi-line comment
that spans multiple lines */
?>
<?php
// Using print statement to send content to the browser
print "Hello, world!";
?>
For example:
// Integer
$age = 30;
// Float
$price = 3.99;
// String
$name = "John";
// Boolean
$is_admin = true;
// Array
$colors = array("red", "green", "blue");
// Object
class Person {
public $name;
public $age;
}
// NULL
$null_variable = null;
Keywords IN PHP:
Control Structures:
● if, else, elseif: Used for conditional statements.
● switch, case, break, default: Used for switch statements.
● while, do, for, foreach: Used for loop control.
● continue, break: Used to control loop execution.
Namespace Declaration:
● namespace, use: Used to declare namespaces and import namespaces
Error Handling:
● try, catch, finally: Used for exception handling.
● throw: Used to throw exceptions.
Variable Scope:
● global: Used to access global variables within a function.
● static: Used to declare static variables within a function.
Other:
● echo, print: Used to output data to the browser or console.
● const: Used to declare constants.
● define: Used to define constants.
● unset: Used to unset a variable.
● empty: Used to check if a variable is empty.
In PHP, variables are used to store and manipulate data. Variable names in PHP must
start with a dollar sign ($) followed by the name of the variable. Here's how you can
use variables in PHP:
Variable Declaration:You can declare variables in PHP using the following syntax:
$variable_name = value;
function myFunction() {
$localVar = "I am local";
echo $globalVar; // Accessing global variable inside function
}
myFunction();
$name = "John";
echo "Hello, $name!"; // Output: Hello, John!
$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName;
Variable Output: You can output variables using echo, print, or by including
them within a string.
$name = "John";
echo "Hello, $name!"; // Output: Hello, John!
Constants in PHP:
In PHP, constants are similar to variables, but they hold values that cannot be
changed once they're defined. Constants are particularly useful for storing
configuration settings
<?php
define("PI", 3.14);
define("SITE_NAME", "My Website");
?>
Using Constants: Once defined, you can use constants anywhere in your
script without the need for a dollar sign “$”.
<?php
echo PI; // Output: 3.14
echo SITE_NAME; // Output: My Website
?>
Case Sensitivity: By default, constant names are case-sensitive.
function myFunction()
{
echo PI;
}
myFunction(); // Output: 3.14
<?php
echo __FILE__; // Outputs the current file's path
echo __LINE__; // Outputs the current line number
?>
Expressions in PHP:
In PHP, expressions are combinations of values, variables, operators,
and function calls that evaluate to a single value. PHP supports a wide range
of expressions, including arithmetic expressions, comparison expressions,
logical expressions, string expressions, and more.
$result = 10 + 5; // Addition
$result = 10 - 5; // Subtraction
$result = 10 * 5; // Multiplication
$result = 10 / 5; // Division
Arithmetic Operators:
● Addition “+”
● Subtraction “-”
● Multiplication “*”
● Division “/”
● Modulus % (returns the remainder of a division)
Assignment Operators:
● Assignment =
● Addition assignment +=
● Subtraction assignment -=
● Multiplication assignment *=
● Division assignment /=
● Modulus assignment %=
● Concatenation assignment .= (for strings)
● Exponentiation assignment **=
Comparison Operators:
● Equal to ==
● Identical to === (compares both value and type)
● Not equal to != or <>
● Not identical to !==
● Greater than >
● Less than <
● Greater than or equal to >=
● Less than or equal to <=
Logical Operators:
String Operators:
○ Concatenation .
○ Concatenation assignment .= (similar to += for strings)
Increment/Decrement Operators:
○ Increment ++
○ Decrement --