Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔹 1. Introduction
Arrays and functions are two essential building blocks in
PHP.
This chapter introduces both — starting from basic arrays to
building your own functions.
🔹 2. Types of Arrays in
PHP
PHP supports three main types of arrays:
✅ 1. Indexed Arrays
$colors
= ["Red", "Green", "Blue"];
echo
$colors[0]; // Red
✅ 2. Associative Arrays
$person
= [
  "name" => "Alice",
  "age" => 28
];
echo
$person["name"]; // Alice
✅ 3. Multidimensional Arrays
$users
= [
  ["Alice",
"alice@gmail.com"],
  ["Bob", "bob@yahoo.com"]
];
echo
$users[1][0]; // Bob
🔹 3. Looping Through
Arrays
✅ Indexed Array:
$fruits
= ["Apple", "Banana", "Mango"];
foreach($fruits
as $fruit){
  echo $fruit . "<br>";
}
✅ Associative Array:
$person
= ["name" => "John", "age" => 30];
foreach($person
as $key => $value){
  echo "$key: $value<br>";
}
🔹 4. Common Array
Functions
| Function | Description | 
| count() | Returns
  number of elements | 
| array_push() | Adds
  element(s) to end of array | 
| array_pop() | Removes and
  returns last element | 
| array_merge() | Merges two or
  more arrays | 
| in_array() | Checks if
  value exists in array | 
| array_keys() | Returns all
  keys of an array | 
| sort() | Sorts indexed
  array | 
| asort() | Sorts
  associative array by value | 
| ksort() | Sorts associative
  array by key | 
✅ Example:
$nums
= [5, 3, 8];
sort($nums);
print_r($nums);
// [3, 5, 8]
🔹 5. Defining and Using
Functions
✅ Basic Syntax:
function
greet() {
  echo "Hello!";
}
greet();
// Output: Hello!
✅ Function With Parameters:
function
sayHello($name) {
  echo "Hi, $name!";
}
sayHello("Jane");
✅ Default Parameters:
function
welcome($name = "Guest") {
  echo "Welcome, $name!";
}
welcome();
// Welcome, Guest!
✅ Return Values:
function
add($a, $b){
  return $a + $b;
}
$result
= add(3, 5);
echo
$result; // 8
🔹 6. Function Scope:
Local vs Global
Variables declared inside functions are local to that
function.
✅ Example:
$name
= "Global";
function
testScope(){
  $name = "Local";
  echo $name;
}
testScope();
// Local
echo
$name;  // Global
✅ Use global keyword:
$x
= 5;
$y
= 10;
function
sum(){
  global $x, $y;
  echo $x + $y;
}
sum();
// 15
🔹 7. Anonymous Functions
& Callbacks
✅ Assigning functions to
variables:
$greet
= function($name) {
  return "Hello, $name!";
};
echo
$greet("Jake");
✅ Using callbacks in array
functions:
$nums
= [1, 2, 3];
$squared
= array_map(function($n){ return $n*$n; }, $nums);
print_r($squared);
// [1, 4, 9]
🔹 8. Summary Table:
Arrays & Functions
| Feature | Example | 
| Indexed Array | $arr =
  [1,2,3]; | 
| Associative
  Array | $user =
  ["name" => "Tom"]; | 
| Loop through
  array | foreach($arr
  as $val) | 
| Count
  elements | count($arr) | 
| Define
  function | function
  sayHi(){} | 
| Return value | return $val; | 
| Default
  argument | function
  foo($x = 1) | 
| Anonymous
  function | $fn =
  function($x){ return $x; }; | 
PHP is primarily used for creating dynamic web pages and server-side applications such as login systems, e-commerce platforms, and CMSs.
✅ Absolutely. PHP continues to power most of the web and is essential in WordPress, Laravel, and web hosting environments.
Yes — you can install XAMPP, MAMP, Laragon, or PHP CLI for local development.
MySQL
     is the most commonly used with PHP, but it also supports PostgreSQL,
     SQLite, and others.
PHP is a server-side scripting language, while JavaScript is primarily client-side, running in the browser.
✅ Yes — PHP is often embedded inside HTML to create dynamic pages
PHP
     files have the .php extension and are executed on the server.
Use method="POST" or method="GET" in your form and access data in PHP using $_POST or $_GET.
✅ Yes — PHP can be used to build RESTful APIs, especially with frameworks like Laravel or Slim.
Laravel, Symfony, CodeIgniter, Zend, and Slim are among the most used PHP frameworks.
 
                Please log in to access this content. You will be redirected to the login page shortly.
Login 
                        Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
 
                        Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)