0% found this document useful (0 votes)
22 views8 pages

PHP Notes

The document outlines the advantages and features of PHP, including its ease of learning, user-friendliness, and support for various databases. It covers PHP syntax, variable declaration rules, data types, and constants, providing examples for each. Additionally, it explains the different types of data in PHP, including scalar, compound, and special types, along with their syntax and usage.

Uploaded by

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

PHP Notes

The document outlines the advantages and features of PHP, including its ease of learning, user-friendliness, and support for various databases. It covers PHP syntax, variable declaration rules, data types, and constants, providing examples for each. Additionally, it explains the different types of data in PHP, including scalar, compound, and special types, along with their syntax and usage.

Uploaded by

saeedarwatkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Describe advantages/features of PHP.

 Easy to learn
 Familiar with syntax
 User friendly
 Support all the leading databases.
 Efficiency in performance
 Platform Independent
 Secure

2) Write syntax of PHP.


A PHP script starts with the tag <?php and ends with the tag ?>. Every PHP statement ends with a semicolon.

PHP file is saved with the extension (.php).

Syntax:

<?php

echo “Hello World”;

?>

3) Variables
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.

Syntax:

$variable_name = value;

Example

<?php

$name = "XYZ"; // String

$age = 30; // Integer

$salary = 45000.50; // Float


$isEmployed = true; // Boolean

?>

Rules:

a. A variable starts with the $ sign, followed by the name of the variable.

b. A variable name must start with a letter or the underscore character.

c. A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an
underscore ($first_name), or with capitalization ($firstName).

d. Variables used before they are assigned have default values.

e. A variable name cannot start with a number.

f. A variable name can only contain alpha-numeric characters (AZ, a-z) and underscores.

g. Variable names are case-sensitive ($name and $NAME are two different variables)

h. Variables can, but do not need, to be declared before assignment. PHP automatically converts the variable to the
correct data type, depending on its value.

i. Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a
number or a string of character

4) Data types
Data types are used to hold different types of data or values. PHP is a loosely typed language, which means variables do
not need to be declared with a specific data type. PHP supports eight different types of data types that can be
categorized in three types.
1. Scalar Types

1.1 Integer

An integer is a whole number without a decimal point. It can be positive or negative. The size of an integer depends on
the platform where PHP runs.

Syntax:
$variable_name = integer_value;

Example:
<?php
$count = 0;
$max = 1000;
$page_size = 10;
?>

Output:

No direct output, but these variables store integer values.

1.2 Float

A float (or double) is a number that contains a decimal point or is written in exponential form. PHP follows the IEEE 754
double format for float representation.

Syntax:
$variable_name = float_value;

Example:
<?php
$price = 10.25;
$tax = 0.08;
?>

Output:

No direct output, but these variables store float values.

1.3 Boolean

A boolean represents a truth value that can be either true or false. PHP treats certain values as false in boolean contexts.

Syntax:
$variable_name = true; // or false
Example:
<?php
$is_admin = true;
$is_user_logged_in = false;
?>

Output:

No direct output, but these variables store boolean values.

Falsy Values in PHP:

PHP considers the following values as false:

 false
 0 (integer and float 0.0)
 Empty string "" or "0"
 Empty array []
 null
 SimpleXML objects from empty elements

All other values are treated as true.

1.4 String

A string is a sequence of characters enclosed in single (') or double (") quotes.

Syntax:
$variable_name = "string_value"; // or 'string_value'

Example:
<?php
$str = 'PHP scalar type';
$message = "PHP data types";
?>

Output:

No direct output, but these variables store string values.

2. Compound Types

2.1 Array

An array is a collection of multiple values stored under a single variable. PHP supports indexed arrays (numeric keys) and
associative arrays (string keys).
Syntax:
$array_name = [value1, value2, value3]; // Indexed array
$array_name = ['key1' => value1, 'key2' => value2]; // Associative array

Example (Indexed Array):


<?php
$carts = ['laptop', 'mouse', 'keyboard'];
echo $carts[0]; // 'laptop'
echo $carts[1]; // 'mouse'
echo $carts[2]; // 'keyboard'
?>

Output:
laptop
mouse
keyboard

Example (Associative Array):


<?php
$prices = [
'laptop' => 1000,
'mouse' => 50,
'keyboard' => 120
];
echo $prices['laptop']; // 1000
echo $prices['mouse']; // 50
echo $prices['keyboard']; // 120
?>

Output:
1000
50
120

2.2 Object

An object is an instance of a class. It has properties (variables) and methods (functions).

Syntax:
class ClassName {
public $property_name;
public function method_name() {
// Code here
}
}
$object = new ClassName();

Example:
<?php
class Person {
public $firstName;
public $lastName;
public function getFullName() {
return $this->firstName . " " . $this->lastName;
}
}

$person = new Person();


$person->firstName = "John";
$person->lastName = "Doe";

echo $person->getFullName();
?>

Output:
John Doe

3. Special Types

3.1 Null

The null type represents a variable with no value. A variable is null if:

 It is explicitly assigned null


 It has not been assigned any value
 It was unset using unset()

Syntax:
$variable_name = null;

Example:
<?php
$var = null;
echo $var; // No output
?>

Output:

(No output as the variable has no value)

3.2 Resource

A resource is a special type that holds references to external resources like database connections, file handles, etc.

Syntax:
$variable_name = fopen("file.txt", "r");

Example (File Handling Resource):


<?php
$file = fopen("example.txt", "r");
if ($file) {
echo "File opened successfully.";
fclose($file);
}
?>

Output:
File opened successfully.

5) Constants
Constants in PHP are identifiers or names that can be assigned any fixed values and cannot change during the execution
of a program. They remain constant throughout the program and cannot be changed during the execution. Once a
constant is defined, it cannot be undefined or redefined.

By convension, constant identifiers are always written in upper case. By default, a constant is always case-sensitive. A
constant name must never start with a number. It always starts with a letter or underscores, followed by letter, numbers
or underscore. It should not contain any special characters except underscore, as mentioned.

Creating a Constant using define() Function

The define() function in PHP is used to create a constant as shown below:

Syntax

define( 'CONSTANT_NAME', value, case_insensitive )

The parameters are as follows:

 name: The name of the constant.


 value: The value to be stored in the constant.
 case_insensitive: Defines whether a constant is case insensitive. By default this value is False, i.e., case
sensitive.

Example: Creating constants using define() function.

<?php

// This creates a case-sensitive constant


define("WELCOME", "GeeksforGeeks");
echo WELCOME . "\n";

// This creates a case-insensitive constant


define("HELLO", "GeeksforGeeks", true);
echo hello;

?>

Output
GeeksforGeeks
GeeksforGeeks
Creating a Constant using const Keyword

The const keyword is another way to define constants but is typically used inside classes and functions. The key
difference from define() is that constants defined using const cannot be case-insensitive.

Syntax

const CONSTANT_NAME = value;

Example:

<?php
const SITE_NAME = 'GeeksforGeeks';
echo SITE_NAME;
?>
Output
GeeksforGeeks

You might also like