0% found this document useful (0 votes)
11 views6 pages

PHP &mysql

The document provides an overview of PHP, a server-side scripting language, and its integration with MySQL for dynamic web development. It covers the basics of PHP syntax, variables, arrays, and operators, along with examples and rules for writing PHP code. Additionally, it explains the benefits of using PHP and MySQL together for creating dynamic websites and applications.

Uploaded by

Shivani Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

PHP &mysql

The document provides an overview of PHP, a server-side scripting language, and its integration with MySQL for dynamic web development. It covers the basics of PHP syntax, variables, arrays, and operators, along with examples and rules for writing PHP code. Additionally, it explains the benefits of using PHP and MySQL together for creating dynamic websites and applications.

Uploaded by

Shivani Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

03610261 - PHP & MYSQ

SELF STUDY MATERIAL


UNIT : 1

Definition of PHP
PHP stands for Hypertext Preprocessor (originally Personal Home Page).
It is an open-source, server-side scripting language designed primarily for web development,
but also used as a general-purpose programming language.

 Server-side means PHP scripts are executed on the web server, and the output (usually
HTML) is sent to the client’s browser.
 PHP can be embedded within HTML, making it very easy to integrate logic into web
pages.

Example:

<?php
echo "Hello, World!";
?>

Benefits of Using PHP & MySQL


Feature PHP MySQL
Type Server-side scripting language Relational Database Management
System (RDBMS)
Open Source Yes Yes
Platform Runs on Windows, Linux, Works on all platforms
Independent macOS
Easy Integration Works with HTML, CSS, Easily integrated with PHP
JavaScript
Performance Fast execution due to server-side Quick data storage and retrieval
processing
Security Supports data encryption, Supports user authentication
hashing
Community Huge online community Widely supported and documented
Support
Cost Effective Free to use Free to use

Why PHP + MySQL Together?

 PHP can connect to MySQL to store and retrieve data dynamically.


 Ideal for dynamic websites, e-commerce, and content management systems (like
WordPress).

Write Your First PHP Program


Steps:

1. Install a local server environment (like XAMPP, WAMP, or LAMP).


2. Save your file with .php extension (e.g., first.php).
3. Store it in the server’s root directory (htdocs for XAMPP).
4. Run it in the browser:
http://localhost/first.php

Example:

<?php
echo "Welcome to PHP Programming!";
?>

Output:

Welcome to PHP Programming!

PHP Structure and Syntax


A PHP script starts with <?php and ends with ?>.

Basic Structure:

<?php
// This is a PHP script
echo "Hello, Shivani!";
?>

Important Rules:

 Statements end with a semicolon (;).


 PHP code can be written anywhere inside an HTML page.
 PHP is case-sensitive for variables but not for function names or keywords.

5. Rules of PHP Syntax


1. Every statement must end with a semicolon (;).
2. PHP code must be enclosed within <?php ... ?>.
3. Variables must start with a $ symbol.
4. Comments:
5. // Single-line comment
6. # Another single-line comment
7. /* Multi-line comment */
8. String Handling:
o " " (double quotes): variables are parsed.
o ' ' (single quotes): variables are not parsed.

6. Integrating HTML with PHP


You can easily mix HTML and PHP in the same file.

Example:

<!DOCTYPE html>
<html>
<body>
<h2>PHP Embedded in HTML</h2>
<?php
$name = "Shivani";
echo "<p>Welcome, $name!</p>";
?>
</body>
</html>

Output:

PHP Embedded in HTML


Welcome, Shivani!

7. Constants in PHP
Constants are like variables, but their values cannot be changed once defined.

Syntax:

define("SITE_NAME", "My PHP Website");


echo SITE_NAME;

Features:

 Do not use $ before the name.


 Global in scope.
 Case-sensitive (unless specified).

8. Variables in PHP
A variable is used to store data like text or numbers.
Rules:

 Start with $.
 Must begin with a letter or underscore.
 Case-sensitive.
 No need to declare data type (PHP is loosely typed).

Example:

<?php
$name = "Shivani";
$age = 21;
echo "Name: $name, Age: $age";
?>

Static Variables

Used inside functions to retain value between function calls.

<?php
function counter() {
static $count = 0;
$count++;
echo $count . "<br>";
}
counter();
counter();
counter();
?>

Output:

1
2
3

Global Variables

Declared outside functions and used inside functions with the global keyword.

<?php
$a = 5;
$b = 10;

function sum() {
global $a, $b;
echo $a + $b;
}

sum();
?>
Output:

15

9. Arrays in PHP
An array is a collection of multiple values in a single variable.

Types of Arrays

Indexed Array (Numeric index)

$colors = array("Red", "Green", "Blue");


echo $colors[0]; // Red

Associative Array (Named keys)

$marks = array("Shivani"=>95, "Aarya"=>89, "Riya"=>92);


echo $marks["Shivani"]; // 95

Multidimensional Array
Arrays inside arrays.

$students = array(
array("Shivani", 95, "A"),
array("Aarya", 89, "B"),
array("Riya", 92, "A")
);

echo $students[0][0]; // Shivani

10. Operators in PHP


Operators are used to perform operations on variables and values.

Types of Operators
Type Example Description
Arithmetic + - * / % Basic math operations
Assignment = += -= *= /= Assign or update values
Comparison == != > < >= <= Compare two values
Logical `&&
String . Concatenate strings
Increment/Decrement ++ -- Increase or decrease value
Array + == === != <> !== Compare arrays
Conditional (Ternary) ?: Short form of if-else

Example:

$a = 10;
$b = 5;
echo $a + $b; // 15
echo ($a > $b) ? "A is greater" : "B is greater";

You might also like