You are on page 1of 12

Feature implode Function explode Function

Description Joins array elements into a string Splits a string into an array based on a delimiter
Syntax string implode ( string $glue , array $pieces ) array explode ( string $delimiter , string $string [, int $limit =
PHP_INT_MAX ] )
Parameters $glue: Specifies the glue between array $delimiter: Specifies the delimiter to split the string
elements
$pieces: The array containing elements to $string: The input string to be split
join
Return Returns a string containing joined array Returns an array of substrings split from the input string
Value elements
Example ```php ```php
$colors = array("Red", "Green", "Blue"); $text = "Hello, World, Welcome";
$result = implode(", ", $colors); $words = explode(", ", $text);
echo $result; print_r($words);
``` ```
Output Red, Green, Blue Array
(
[0] => Hello
[1] => World
[2] => Welcome
)

Traversing arrays refers to the process of accessing and iterating through the elements of an array to perform operations on each
element. In PHP, there are several methods to traverse arrays effectively:

1. Using foreach Loop:


The `foreach` loop is commonly used to iterate over each element in an array without explicitly handling array indices. It
simplifies the process of accessing array elements and is especially useful for associative and multidimensional arrays.
2. Using for Loop with count():
For indexed arrays, you can use a `for` loop along with the `count()` function to iterate through each element based on its index.

3. Using while Loop:


While loops can also be used to traverse arrays, although they are less commonly used compared to foreach and for loops.

4. Using array_walk():
The `array_walk()` function allows you to apply a user-defined function to each element of an array. It's useful for performing
custom operations during traversal.
1. Defining a Function (`function` keyword):
- In PHP, a function is defined using the `function` keyword, followed by the function name and parentheses containing optional
parameters.
- The function name should start with a letter or underscore, followed by letters, numbers, or underscores.
- Parameters are variables that hold values passed to the function.
- The function body contains the code that performs specific tasks when the function is called.

<?php
function functionName($param1, $param2) {
// Function body
// Perform operations using $param1 and $param2
}
?>

2. Calling a Function:
- Once a function is defined, it can be called or invoked to execute the code inside its function body.
- To call a function, simply use its name followed by parentheses, optionally passing arguments inside the parentheses if the
function expects parameters.

<?php
functionName($value1, $value2);
?>

3. Function Arguments:
- Function arguments are the values passed to a function when it is called.
- Parameters are placeholders for these values within the function definition.
- Arguments can be variables, constants, expressions, or even other function calls.
4. Returning Values (`return` statement):
- Functions can return values using the `return` statement.
- The `return` statement exits the function and sends the specified value back to the calling code.
- A function can return only one value, but that value can be a single variable, an array, an object, etc.

5. Function Body:
- The function body contains the code that defines the behavior of the function.
- It includes statements, expressions, control structures, and function calls needed to perform the desired task.
- The function body executes when the function is called, based on the logic defined inside it.
1. User Defined Functions:

- Description: User-defined functions are functions created by the user using the `function` keyword followed by a function name
and parameters. These functions allow developers to encapsulate a set of code that can be reused multiple times throughout the
program.

2. Variable Functions:

- Description: Variable functions in PHP allow the use of a string containing the function name as a variable to call the function
dynamically. This is useful when the function name needs to be determined at runtime, such as calling different functions based
on conditions.

3. Anonymous Functions (Lambda Functions):


- Description: Anonymous functions, also known as lambda functions, are functions defined without a name using the `function`
keyword and assigned to a variable. They are useful for one-time or short-lived tasks where defining a separate function is
unnecessary. Anonymous functions can capture variables from the parent scope using the `use` keyword.

1. str_word_count() Function:

- Description: The `str_word_count()` function counts the number of words in a string.

2. strlen() Function:

- Description: The `strlen()` function returns the length of a string in terms of the number of characters.
3. strrev() Function:

- Description: The `strrev()` function reverses a string.

4. strpos() Function:

- Description: The `strpos()` function finds the position of the first occurrence of a substring within a string.

5. strrpos() Function:

- Description: The `strrpos()` function finds the position of the last occurrence of a substring within a string.
6. str_replace() Function:

- Description: The `str_replace()` function replaces all occurrences of a substring with another substring in a string.

7. ucwords() Function:

- Description: The `ucwords()` function capitalizes the first letter of each word in a string.

8. strtoupper() Function:

- Description: The `strtoupper()` function converts a string to uppercase.

9. strtolower() Function:
- Description: The `strtolower()` function converts a string to lowercase.

10. strcmp() Function:

- Description: The `strcmp()` function compares two strings and returns 0 if they are equal, -1 if the first string is less than the
second, and 1 if the first string is greater than the second.

Here are examples of basic graphics concepts in PHP, including creating images, adding text to images, scaling images, and
creating a PDF document, presented as executable code blocks within `<?php ... ?>` tags:

1. Creating Images:

- Description: This code creates a blank image with dimensions 400x200 pixels, sets the background color to white, saves the
image as a PNG file named 'image.png', and then frees up memory.

2. Images with Text:


- Description: This code creates an image with text "Hello, PHP Graphics!" using the GD library's `imagettftext()` function. It sets
the font size to 20, font angle to 0 degrees, and text position at (50, 100) on the image.

3. Scaling Images:

- Description: This code loads an existing image, scales it to 50% of its original size using the `imagescale()` function, and saves the
scaled image as a PNG file named 'scaled_image.png'.
4. Creation of PDF Document:

- Description: This code uses the FPDF library to create a PDF document with the text "Hello, PHP PDF!" in Arial bold font and
saves the PDF as 'pdf_document.pdf' in the 'output' directory.

You might also like