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

PHP Notes

The document discusses PHP data types including integer, float, string, boolean, array, object, NULL, and resource. It provides examples of each data type and discusses switch statements, foreach loops, implode() and explode() functions, constructors, destructors, and inheritance in PHP.

Uploaded by

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

PHP Notes

The document discusses PHP data types including integer, float, string, boolean, array, object, NULL, and resource. It provides examples of each data type and discusses switch statements, foreach loops, implode() and explode() functions, constructors, destructors, and inheritance in PHP.

Uploaded by

Om Kawate
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PHP NOTES (22619)

CH.1
Q1. Data Types

1. **Integer**: Represents whole numbers, positive or negative, without


decimals.

2. **Float (or Double)**: Represents numbers with decimal points. They


can also be expressed in scientific notation.

3. **String**: Represents sequences of characters, like text. Strings can


be enclosed in single quotes ('') or double quotes ("").

4. **Boolean**: Represents either true or false.

5. **Array**: Represents an ordered map that stores key-value pairs.


Keys can be integers or strings.

6. **Object**: Represents instances of user-defined classes. Objects


contain both data and functions.

7. **NULL**: Represents a variable with no value assigned.

8. **Resource**: Represents external resources such as database


connections or file handles. It's a special data type used to handle
external entities.

Certainly, here are examples illustrating each data type in PHP:

1. **Integer**:

```php

$age = 30;

$count = -5;

2. **Float (or Double)**:

```php

$price = 19.99;
$pi = 3.14159;

```

3. **String**:

```php

$name = "John Doe";

$message = 'Hello, World!';

```

4. **Boolean**:

```php

$is_active = true;

$has_permission = false;

```

5. **Array**:

```php

$colors = array("red", "green", "blue");

$person = array("name" => "John", "age" => 30, "city" => "New York");

```

6. **Object**:

```php

class Car {

public $brand;

public $model;

public function __construct($brand, $model) {

$this->brand = $brand;
$this->model = $model;

$car = new Car("Toyota", "Corolla");

```

7. **NULL**:

```php

$no_value = null;

```

8. **Resource**:

```php

$file_handle = fopen("example.txt", "r");

// $file_handle is a resource representing a file handle

```
 Switch Statement

The "switch" statement in PHP is a control structure used for decision-


making. It provides a way to select one choice from many possible
options based on the value of an expression. It's an alternative to a
series of "if-elseif-else" statements, particularly when you have a large
number of conditions to check.

Here's the basic syntax of a switch statement:

```php

switch (expression) {
case value1:

// code to be executed if expression matches value1

break;

case value2:

// code to be executed if expression matches value2

break;

// additional cases as needed

default:

// code to be executed if expression doesn't match any case

```

- The "expression" is evaluated once and compared with the values in


the "case" clauses.

- If a match is found, the code block following that case is executed until
a "break" statement is encountered.

- If no match is found, the code block following the "default" case (if
provided) is executed.

Here's an example to illustrate:

```php

$day = "Monday";

switch ($day) {

case "Monday":

echo "Today is Monday";

break;
case "Tuesday":

echo "Today is Tuesday";

break;

case "Wednesday":

echo "Today is Wednesday";

break;

default:

echo "It's not Monday, Tuesday, or Wednesday";

```

 Foreach loop

PHP | foreach Loop


Last Updated : 25 Sep, 2019



The foreach construct provides the easiest way to iterate the array elements. It
works on array and objects both. The foreach loop though iterates over an array of
elements, the execution is simplified and finishes the loop in less time
comparatively. It allocates temporary memory for index iterations which makes the
overall system to redundant its performance in terms of memory allocation.
Syntax:
foreach( $array as $element ) {
// PHP Code to be executed
}
CH.2
Q1. Explain implode() and explode()

In PHP, `implode()` and `explode()` are two functions commonly used for
handling strings:

1. **implode()**: This function takes an array of strings and concatenates


them into a single string, using a specified delimiter between each
element.

```php

<?php

$array = array('Hello', 'World', '!');

$string = implode('-', $array);

echo $string; // Output: Hello-World-!

?>

```

In this example, `implode('-', $array)` joins the elements of the `$array`


with the hyphen `-` between each element.

2. **explode()**: This function breaks a string into an array of substrings


based on a specified delimiter.

```php

<?php

$string = "apple,banana,orange";

$array = explode(',', $string);

print_r($array); // Output: Array ( [0] => apple [1] => banana [2] =>
orange )

?>

```
Here, `explode(',', $string)` splits the `$string` at each comma `,` and
stores the substrings into the `$array`.

These functions are particularly useful for converting strings to arrays


and vice versa, which is a common requirement when working with data
in PHP, especially when dealing with CSV files, database results, or
HTTP query parameters.
CH.3
 Constructors

1. Constructor*

In object-oriented programming, a constructor is a special type of


method that is automatically called when an object of a class is created.
Its primary purpose is to initialize the newly created object. In PHP, the
constructor method is defined using the `__construct()` keyword.

Here's a basic example:

```php

<?php

class MyClass {

public function __construct() {

echo "Constructor called!";

$obj = new MyClass(); // Output: Constructor called!

?>

In this example, when an object `$obj` of the `MyClass` is created, the


constructor `__construct()` is automatically invoked, and it echoes
"Constructor called!".

 Destructors

PHP - The __destruct Function


A destructor is called when the object is destructed or the script is stopped or
exited.

If you create a __destruct() function, PHP will automatically call this function
at the end of the script.

Notice that the destruct function starts with two underscores (__)!
The example below has a __construct() function that is automatically called
when you create an object from a class, and a __destruct() function that is
automatically called at the end of the script:

Example
<?php
class Fruit {
public $name;
public $color;

function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}

$apple = new Fruit("Apple");


?>

 Inheritance
 Inheritance in OOP = When a class derives from another class.
 The child class will inherit all the public and protected properties and
methods from the parent class. In addition, it can have its own
properties and methods.
 An inherited class is defined by using the extends keyword.

You might also like