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

conditions

The document explains various control structures in PHP for creating conditions, including 'if', 'else', 'else if', 'switch', and the ternary operator. It also covers arrays, detailing indexed, associative, and multidimensional arrays, along with common array functions. The content provides examples for each concept to illustrate their usage in PHP programming.

Uploaded by

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

conditions

The document explains various control structures in PHP for creating conditions, including 'if', 'else', 'else if', 'switch', and the ternary operator. It also covers arrays, detailing indexed, associative, and multidimensional arrays, along with common array functions. The content provides examples for each concept to illustrate their usage in PHP programming.

Uploaded by

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

In PHP, conditions are created using various control structures that allow you to execute different

blocks of code based on whether a specified condition evaluates to true or false. The most commonly
used control structures for conditions in PHP are `if`, `else`, `else if`, `switch`, and `ternary operators`.

### 1. **If Statement**


The `if` statement executes a block of code if a specified condition is true.

```php
$age = 18;

if ($age >= 18) {


echo "You are an adult.";
}
```

### 2. **Else Statement**


The `else` statement can follow an `if` statement to provide an alternate block of code when the
condition is false.

```php
$age = 16;

if ($age >= 18) {


echo "You are an adult.";
} else {
echo "You are a minor.";
}
```

### 3. **Else If Statement**


The `else if` statement allows you to check multiple conditions.

```php
$age = 20;

if ($age < 13) {


echo "You are a child.";
} elseif ($age >= 13 && $age < 20) {
echo "You are a teenager.";
} else {
echo "You are an adult.";
}
```

### 4. **Switch Statement**


The `switch` statement provides a way to execute one block of code from multiple conditions. It's
often used when you need to check a variable against multiple values.

```php
$fruit = "apple";

switch ($fruit) {
case "apple":
echo "You chose an apple.";
break;
case "banana":
echo "You chose a banana.";
break;
case "orange":
echo "You chose an orange.";
break;
default:
echo "Unknown fruit.";
}
```

### 5. **Ternary Operator**


The ternary operator is a shorthand for the `if-else` statement. It takes three operands: a condition, a
result for true, and a result for false.

```php
$age = 18;

$result = ($age >= 18) ? "Adult" : "Minor";


echo $result; // Outputs: Adult
```

### 6. **Comparisons**
You can use a variety of comparison operators in your conditions:

- `==` (equal to)


- `===` (identical to)
- `!=` (not equal to)
- `!==` (not identical to)
- `>` (greater than)
- `<` (less than)
- `>=` (greater than or equal to)
- `<=` (less than or equal to)

### 7. **Logical Operators**


You can also combine conditions using logical operators:

- `&&` (and)
- `||` (or)
- `!` (not)

### Example of Combining Conditions


```php
$age = 25;
$hasID = true;

if ($age >= 18 && $hasID) {


echo "You can enter the club.";
} else {
echo "You cannot enter the club.";
}
```

### Summary
Using these structures, you can control the flow of your PHP scripts based on various conditions,
allowing for more dynamic and responsive applications. Remember to ensure that your comparisons
and logical conditions are correctly structured to avoid unexpected behavior.
Arrayys
In PHP, arrays are a fundamental data structure that allow you to store
multiple values in a single variable. There are two main types of arrays in
PHP: indexed arrays and associative arrays.
1. Indexed Arrays
Indexed arrays are arrays where each element is accessed using a numerical
index, starting from 0.
Creating Indexed Arrays

Copy $fruits = array("Apple", "Banana",


"Cherry");// Or using shorthand syntax$fruits = ["Apple", "Banana",
"Cherry"];

Accessing Elements

Copy echo $fruits[0]; // Outputs:


Appleecho $fruits[1]; // Outputs: Banana

Modifying Elements

Copy $fruits[1] = "Blueberry"; // Change


Banana to Blueberry

Adding Elements

Copy $fruits[] = "Date"; // Adds Date to


the end of the array

2. Associative Arrays
Associative arrays allow you to use named keys that you assign to each
element, instead of using numeric indexes.
Creating Associative Arrays

Copy $ages = array("John" => 25, "Jane"


=> 30, "Doe" => 28);// Or using shorthand syntax$ages = ["John" => 25,
"Jane" => 30, "Doe" => 28];

Accessing Elements

Copy echo $ages["John"]; // Outputs:


25echo $ages["Jane"]; // Outputs: 30

Modifying Elements
Copy $ages["Jane"] = 31; // Change
Jane's age from 30 to 31

Adding Elements

Copy $ages["Mike"] = 22; // Adds Mike


with age 22

3. Multidimensional Arrays
Multidimensional arrays are arrays containing one or more arrays.
Creating Multidimensional Arrays

Copy $cars = [
["Volvo", 22, 18],
["BMW", 15, 13],
["Toyota", 5, 2]
];

Accessing Elements

Copy echo $cars[0][0]; // Outputs:


Volvoecho $cars[1][1]; // Outputs: 15

4. Common Array Functions


PHP provides various built-in functions to work with arrays:
 count($array): Returns the number of elements in an array.
 array_push($array, $value): Adds one or more elements to the end of an
array.
 array_pop($array): Removes the last element from an array.
 array_merge($array1, $array2): Merges one or more arrays.
 in_array($value, $array): Checks if a value exists in an array.
Example Code
Here’s a complete example demonstrating the different types of arrays and
functions:

Copy <?php// Indexed Array$colors =


["Red", "Green", "Blue"];$colors[] = "Yellow"; // Add Yellowecho
$colors[2]; // Outputs: Blue
// Associative Array$pets = ["Dog" => "Bark", "Cat" => "Meow"];echo
$pets["Cat"]; // Outputs: Meow
// Multidimensional Array$students = [
["name" => "Alice", "age" => 21],
["name" => "Bob", "age" => 23]
];echo $students[1]["name"]; // Outputs: Bob
// Using array functions$numbers = [1, 2, 3];array_push($numbers,
4);print_r($numbers); // Outputs: Array ( [0] => 1 [1] => 2 [2] => 3 [3]
=> 4 )

You might also like