Functions and Array
Functions and Array
Functions
1. Defining and Calling Functions:
• Defining Functions: Functions in PHP are
defined using the function keyword, followed
by the function name, parentheses (), and a
block of code enclosed in {}.
2. Function Parameters:
• Functions can take parameters to work with
different inputs.
3. Return Values:
• Functions can return values using the return
statement.
4. Variable Scope:
• Local Scope: Variables defined within a
function are local to that function.
• Global Scope: Variables defined outside any
function have a global scope and can be
accessed anywhere outside functions. To
access them inside functions, use the global
keyword.
5. Built-in Functions:
• PHP comes with many built-in functions for
different tasks, such as strlen() for string
length, array_merge() for merging arrays, etc.
2.Arrays
1. Indexed Arrays:
• Arrays with a numeric index.
2. Associative Arrays:
• Arrays with named keys.
3. Multidimensional Arrays:
• Arrays containing one or more arrays.
4. Array Functions:
• PHP provides various functions for working
with arrays.
o count(): Returns the number of elements
in an array.
o array_merge(): Merges one or more
arrays.
o array_push(): Adds one or more elements
to the end of an array.
o array_pop(): Removes the last element of
an array.