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

PHP Important

The document provides a comprehensive overview of PHP programming, covering its basic syntax, variable declaration, data types, variable scope, and key features. It also explains how to embed PHP in HTML, install local servers like XAMPP and WAMP, and outlines the architecture of dynamic websites using PHP. Additionally, it includes examples and installation steps for setting up a local development environment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PHP Important

The document provides a comprehensive overview of PHP programming, covering its basic syntax, variable declaration, data types, variable scope, and key features. It also explains how to embed PHP in HTML, install local servers like XAMPP and WAMP, and outlines the architecture of dynamic websites using PHP. Additionally, it includes examples and installation steps for setting up a local development environment.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

PHP Programming basic syntax of PHP


 PHP, a powerful server-side scripting language used in web development. It’s
simplicity and ease of use makes it an ideal choice for beginners and experienced
developers.
Basic PHP Syntax
 PHP code is executed between PHP tags, allowing the integration of PHP code within
HTML. The most common PHP tag is <?php … ?>, which is used to enclose PHP
code. The <?php ….?> is called Escaping to PHP.
<?php
// code
?>
 The script starts with <?php and ends with ?>. These tags are also called ‘Canonical
PHP tags’. Everything outside of a pair of opening and closing tags is ignored by the
PHP parser.
 The open and closing tags are called delimiters. Every PHP command ends with a
semi-colon (;).
Example of PHP
<?php
// Here echo command is used to print
echo "Hello, world!";
?>
______________________________________________________________________________
2. Embedding PHP in HTML
 PHP code can be embedded within HTML using the standard PHP tags. In this
example, the <?php echo “Hello, PHP!”; ?> statement dynamically inserts a heading
into the HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Syntax Example</title>
</head>
<body>
<h1><?php echo "Hello, PHP!"; ?></h1>
</body>
</html>
_________________________________________________________________________________
3. PHP Variable
 PHP Variables are one of the most fundamental concepts in programming. It is used to
store data that can be accessed and manipulated within your code.
 Variables in PHP are easy to use, dynamically typed (meaning that you do not need to
declare their type explicitly), and essential for creating dynamic, interactive web
applications.
Declaring Variables in PHP
 To declare a variable in PHP, you simply assign a value to it using the $ symbol
followed by the variable name. PHP variables are case-sensitive and must start with a
letter or an underscore, followed by any number of letters, numbers, or underscores.
Example
<?php
$name = "XYZ"; // String
$age = 30; // Integer
$salary = 45000.50; // Float
$isEmployed = true; // Boolean
?>
Variable Naming Conventions
In PHP, it’s important to follow certain naming conventions for PHP variables to ensure
readability and maintainability:
 Start with a Letter or Underscore: Variable names must begin with a letter or an
underscore (_), not a number.
 Use Descriptive Names: Variable names should be descriptive of their purpose, e.g.,
$userName, $totalAmount.
 Case Sensitivity: PHP variable names are case-sensitive, meaning $name and $Name are
different variables.
 Avoid Reserved Words: Do not use PHP reserved words or keywords as variable names
(e.g., function, class, echo).
Example of Valid and Invalid Variable Names
<?php
$firstName = "Alice"; // Valid
$_age = 25; // Valid
$2ndPlace = "Bob"; // Invalid: Cannot start with a number
$class = "Physics"; // Valid, but avoid reserved words
?>
______________________________________________________________________________
4. Data Types in PHP
 Integers: Integers are whole numbers without decimals, which can be positive or
negative.
 Doubles: Floats are numbers with decimal points or in exponential form.
 NULL: The NULL data type represents a variable with no value.
 Strings: Strings are sequences of characters, enclosed in single (‘ ‘) or double (” “)
quotes.
 Booleans: Booleans represent two possible values: true or false.
 Arrays: Arrays are used to store multiple values in a single variable. Arrays can be
indexed, associative, or multidimensional.
 Objects: Objects are instances of classes, which are templates for creating data
structures with properties and methods.
 Resources: Resources are special variables that hold references to external resources,
such as database connections or file handles.
______________________________________________________________________
5. PHP Variable Scope
 The scope of a variable refers to where it can be accessed within the code. PHP
variables can have local, global, static.
1. Local Scope or Local Variable
 Variables declared within a function have local scope and cannot be accessed outside
the function. Any declaration of a variable outside the function with the same name (as
within the function) is a completely different variable.
Example: This example shows the local variable in PHP.
<?php
$num = 60;
function local_var() {
// This $num is local to this function
// The variable $num outside the function
// is a completely different
$num = 50;
echo "Variable num inside function is: $num \n";
}
local_var();
// The $num outside function is a completely
// different from inside local_var()
echo "Variable num outside function is: $num";
?>
Output
Variable num inside function is: 50

Variable num outside function is: 60

2. Global Scope or Global Variable


The variables declared outside a function are called global variables. These variables can be
accessed directly outside a function. To get access within a function we need to use the
“global” keyword before the variable to refer to the global variable.
Example: This example shows the Global Variables in PHP.
<?php
$num = 20;
// Function to demonstrate use of global variable
function global_var() {
// We have to use global keyword before
// the variable $num to access within
// the function
global $num;

echo "Variable num inside function: $num \n";


}
global_var();
echo "Variable num outside function: $num \n";
?>
Output
Variable num inside function: 20

Variable num outside function: 20

3. Static Variables
It is the characteristic of PHP to delete the variable. Once it completes its execution and the
memory is freed. But sometimes we need to store the variables even after the completion of
function execution. To do this, we use the static keywords and the variables are then called
static variables. PHP associates a data type depending on the value for the variable.
Example: This example shows the Static variable in PHP.
<?php
// Function to demonstrate static variables
function static_var() {
// Static Variable
static $num = 5;
$sum = 2;
$sum++;
$num++;
echo $num, "\n";
echo $sum, "\n";
}
// First function call
static_var();
// Second function call
static_var();

?>
Output
6

__________________________________________________________________________
6. Key features of PHP.
 Key features of PHP include: being open-source and free to use, easy to learn and use,
strong database connectivity, compatibility with HTML, cross-platform functionality, a
large developer community, support for various frameworks, and the ability to create
dynamic web pages; making it a popular choice for web development, especially for
creating dynamic websites and applications.
Breakdown of key features:
Open-source:
PHP is freely available to use and modify, with no licensing costs involved.
Easy to learn:
The syntax is considered relatively simple and straightforward, making it accessible to
beginner programmers.
Database integration:
PHP provides built-in functionality to connect with various databases like MySQL,
PostgreSQL, and Oracle, allowing seamless data management.
HTML embedding:
PHP code can be easily embedded within HTML pages for dynamic content generation.
Cross-platform compatibility:
PHP applications can run on different operating systems without significant modifications.
Large community support:
Due to its widespread use, a large community of PHP developers is available to provide
support and resources.
Frameworks availability:
Popular frameworks like Laravel, CodeIgniter, and Symfony provide structured development
environments for complex projects.
Dynamic web pages:
PHP enables the creation of interactive web pages where content can change based on user
input or other conditions.
Form handling:
PHP can efficiently process form data submitted by users on web pages.
Performance optimization:
With proper coding practices and optimization techniques, PHP can deliver good performance
for web applications.
____________________________________________________________________________
7. Hello world program in php
<!DOCTYPE html>

<html>

<body>

<h1>My PHP Website</h1>

<?php

echo "Hello World!";

?>

</body>

</html>
8. XAMPP and WAMP Installation.

 XAMPP and WAMP are both software packages used to set up a local web server on
your computer, allowing you to test websites and web applications without needing a
remote server; the key difference is that XAMPP is cross-platform (works on Windows,
Mac, Linux), while WAMP is specifically designed for Windows systems only; both
involve downloading an installer, choosing components like Apache web server, MySQL
database, and PHP, and then running the installation wizard to set up the local server on
your machine.
 Key points about XAMPP and WAMP installation:
 What they stand for:
o XAMPP: Cross-Platform Apache, MySQL, PHP, and Perl.
o WAMP: Windows, Apache, MySQL, and PHP.
 Installation process:
o Download: Go to the official website (Apache Friends for XAMPP) and
download the installer for your operating system.
o Run the installer: Double-click the downloaded file to start the setup wizard.
o Select components: Choose which components you want to install (usually
Apache, MySQL, PHP).
o Choose installation directory: Specify where you want to install
XAMPP/WAMP on your computer.
o Install: Click "Next" to proceed through the installation process.
o Start the server: Once installed, open the XAMPP/WAMP control panel and
start the Apache and MySQL services.
o
9. Difference between Xampp and Wamp :

SR.N
XAMPP WAMP
O

It is a cross-platform software package


Its local server is only supported by
1. supported by platforms like Mac OS, Linux,
Windows Operating system.
and Windows.

It is easy to download and install but may It is easy to download and install and
2.
differ for different platforms. also light- weighted.

It uses MySQL, which is an RDBMS


It uses MariaDB, which is an RDBMS for
3. for storing and retrieving operations
storing and retrieving operations on data.
on data.

The programming or scripting languages The programming or scripting


4. used for development in XAMPP are Perl languages used for development in
and PHP. WAMP is PHP.

5. It uses the Apache Web server. It uses the Apache Web server.
It is more powerful and resource taking as It is less powerful and resource taking
6.
compared to WAMP. than XAMPP.

7. It has SSL feature. It does not have SSL feature.

It is available in both 64 bit and 32-bit


8. It is available in 64-bit system only.
system.

10. Architecture of a Dynamic Website Using PHP


 A dynamic website using PHP follows a Client-Server Architecture, typically
consisting of the following components:
1. Client-Side (Front-end)
 The web browser (Google Chrome, Firefox, etc.) sends requests to the web server.
 Uses HTML, CSS, and JavaScript to display content dynamically.
 AJAX can be used to update content asynchronously without refreshing the page.
2. Web Server
 The Apache, Nginx, or IIS server processes the request and forwards it to the PHP
script.
 The web server handles communication between the client and PHP scripts.
3. PHP (Server-Side Scripting)
 PHP code is executed on the server to process the request.
 Retrieves or manipulates data in the database and dynamically generates HTML content.
4. Database (MySQL, PostgreSQL, etc.)
 Stores dynamic content such as user accounts, blog posts, products, etc.
 PHP interacts with the database using SQL queries to retrieve or modify data.
5. Response to the Client
 The PHP script sends the generated HTML page back to the web server.
 The web server delivers the final HTML response to the client's browser.

Working of a Dynamic Website Using PHP


The step-by-step workflow of a PHP-based dynamic website is as follows:
Step 1: User Request
 The user enters a URL in the browser (e.g., www.example.com/profile.php?user=123).
 The browser sends an HTTP request to the web server.
Step 2: Web Server Processing
 The web server receives the request and identifies that the requested page (profile.php) is
a PHP file.
 The web server forwards the request to the PHP interpreter.
Step 3: PHP Script Execution
 The PHP script (profile.php) executes and checks the request parameters (e.g., user=123).
 The script communicates with the database to fetch the requested user’s details.
Step 4: Database Query
 The PHP script sends a SQL query to the database:
sql
CopyEdit
SELECT * FROM users WHERE id = 123;
 The database responds with the user’s information.
Step 5: Dynamic Content Generation
 PHP processes the database result and dynamically generates HTML content:
php
CopyEdit
<h1>Welcome, John Doe</h1>
<p>Email: [email protected]</p>
 This content is returned to the web server.
Step 6: Response to Browser
 The web server sends the dynamically generated HTML page back to the client.
 The browser displays the final page with personalized content
11. Installation steps for xampp and wamp
 To install XAMPP or WAMP, download the installer from the official website, run the
executable, choose the desired components, select the installation directory, and follow
the on-screen prompts to complete the setup; after installation, access the control panel to
start the Apache web server and MySQL database to test functionality by navigating to
"http://localhost" in your web browser.
 Key steps for both XAMPP and WAMP installation:

 Download: Go to the official website (Apache Friends for XAMPP) and download the
appropriate installer for your operating system.

 Run the installer: Double-click the downloaded executable file to launch the setup wizard.

 Select components: Choose which components you want to install (usually including Apache
web server, MySQL database, PHP).

 Choose installation directory: Select the location where you want to install XAMPP/WAMP
(default is usually recommended).

 Install: Click "Next" or "Install" to begin the installation process.

 Launch control panel: Once installed, open the XAMPP/WAMP control panel to start and stop
services.

 Test installation: Open a web browser and navigate to "http://localhost" to see the default
XAMPP/WAMP page, confirming successful installation.
Important Considerations:

 User Account Control (UAC): Depending on your system settings, you may need to grant
administrator privileges during installation.

 Firewall settings: Ensure your firewall allows access to the ports used by Apache and MySQL.

 Port conflicts: If another application is already using the default ports, you may need to adjust
settings within the XAMPP/WAMP control panel.

You might also like