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

PHP

PHP is a server-side scripting language used for web development, allowing dynamic content and database interaction. It features a simple syntax with variables, data types, control structures, functions, and supports object-oriented programming. Key concepts include PHP tags, echo/print for output, superglobals for data handling, and error handling using try-catch blocks.

Uploaded by

Tristan Reyes
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)
3 views

PHP

PHP is a server-side scripting language used for web development, allowing dynamic content and database interaction. It features a simple syntax with variables, data types, control structures, functions, and supports object-oriented programming. Key concepts include PHP tags, echo/print for output, superglobals for data handling, and error handling using try-catch blocks.

Uploaded by

Tristan Reyes
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

PHP (Hypertext Preprocessor) is a server-side scripting language used for web development.

It can be
embedded in HTML to add dynamic content and interact with databases. Let's break down the basic
syntax and semantics:

### 1. **PHP Tags**

PHP code is embedded within HTML using special tags:

```php

<?php

// PHP code goes here

?>

```

### 2. **Variables**

- Variables in PHP start with a dollar sign (`$`).

- They don't require explicit declaration or data type definition.

```php

<?php

$name = "John"; // String

$age = 25; // Integer

$price = 19.99; // Float

?>

```

### 3. **Data Types**

PHP has several primary data types:

- **String**: A sequence of characters (`$text = "Hello";`)

- **Integer**: A non-decimal number (`$num = 10;`)

- **Float**: A decimal number (`$price = 10.5;`)

- **Boolean**: True or False (`$is_valid = true;`)

- **Array**: A collection of values (`$colors = array("Red", "Green", "Blue");`)


- **Object**: Instance of a class (`$car = new Car();`)

### 4. **Echo/Print**

These are used to output data to the browser:

```php

<?php

echo "Hello, World!";

print "PHP is fun!";

?>

```

### 5. **Concatenation**

To join strings together, PHP uses the dot (`.`) operator:

```php

<?php

$greeting = "Hello, " . $name;

echo $greeting;

?>

```

### 6. **Control Structures**

#### **If-Else Statements:**

```php

<?php

if ($age > 18) {

echo "Adult";

} else {

echo "Not an adult";

}
?>

```

#### **Switch Statement:**

```php

<?php

$day = "Monday";

switch ($day) {

case "Monday":

echo "It's Monday!";

break;

case "Tuesday":

echo "It's Tuesday!";

break;

default:

echo "Another day!";

?>

```

#### **Loops:**

- **For loop:**

```php

<?php

for ($i = 0; $i < 5; $i++) {

echo $i;

?>

```
- **While loop:**

```php

<?php

$i = 0;

while ($i < 5) {

echo $i;

$i++;

?>

```

### 7. **Functions**

Functions are reusable blocks of code that perform a specific task:

```php

<?php

function sayHello() {

echo "Hello!";

sayHello();

?>

```

Functions can also accept parameters and return values:

```php

<?php

function add($a, $b) {

return $a + $b;

}
echo add(3, 4); // Outputs 7

?>

```

### 8. **Superglobals**

PHP provides predefined variables accessible from anywhere:

- `$_GET`: Contains data sent via URL parameters.

- `$_POST`: Contains data sent via a form using POST.

- `$_SERVER`: Contains server information like headers and paths.

Example of handling a form submission:

```php

<form method="post" action="submit.php">

Name: <input type="text" name="name">

<input type="submit">

</form>

<?php

// submit.php

$name = $_POST['name'];

echo "Hello, " . $name;

?>

```

### 9. **Arrays**

#### **Indexed Arrays:**

```php

<?php

$fruits = array("Apple", "Banana", "Orange");


echo $fruits[0]; // Outputs "Apple"

?>

```

#### **Associative Arrays:**

```php

<?php

$ages = array("John" => 25, "Jane" => 30);

echo $ages["John"]; // Outputs 25

?>

```

### 10. **Object-Oriented PHP**

PHP supports OOP (Object-Oriented Programming):

```php

<?php

class Car {

public $make;

public $model;

public function __construct($make, $model) {

$this->make = $make;

$this->model = $model;

public function displayInfo() {

echo "Car: " . $this->make . " " . $this->model;

}
$myCar = new Car("Toyota", "Corolla");

$myCar->displayInfo(); // Outputs: Car: Toyota Corolla

?>

```

### 11. **Error Handling**

Basic error handling uses the `try-catch` blocks:

```php

<?php

try {

// Code that may throw an exception

} catch (Exception $e) {

echo "Caught exception: " . $e->getMessage();

?>

```

### 12. **Comments**

PHP supports single-line and multi-line comments:

```php

<?php

// Single-line comment

# Another single-line comment

/*

Multi-line comment

*/

?>

```
### Semantics

- **Interpreted Language:** PHP is executed on the server, and the result is sent to the browser.

- **Loose Typing:** PHP automatically converts data types based on context.

- **Scope:** Variables declared inside a function are local to that function, unless declared global.

You might also like