Associative Arrays:
index.php
<?php
$person = [
'age' => 31,
'hair' => 'brown,
'career' => 'web developer'
];
$person['name'] = 'prasanth'; // add the value to the associative array
unset($person['hair']); // remove the value from the associative array
require(index.blade.php);
?>
index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<?php foreach ($person as $key => $value): ?>
<li><strong><?= $key; ?></strong><?= $value; ?> </li>
<?php endforeach; ?>
</ul>
</body>
</html>
Functions:
<?php
function dd($data){
echo "<pre>";
die(var_dump($data));
echo "</pre>";
}
?>
Classes:
class Task {
protected $description;
protected $completed = false;
public function __construct($description){
$this->description = $description;
}
public function isCompleted(){
return $this->completed;
}
}
$task = new Task('go to store');
var_dump($task->isCompleted());//bool(false)
another example:
index.php
<?php
class Task {
public $description;
protected $completed = false;
public function __construct($description){
$this->description = $description;
}
public function isCompleted(){
return $this->completed;
}
public function complete(){
$this->completed = true;
}
}
$tasks = [
new Task('go to store'),
new Task('go to market'),
new Task('read book')
];
$tasks[1]->complete();
//var_dump($tasks); //bool(false)
require 'index.blade.php';
?>
index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul>
<?php foreach ($tasks as $task): ?>
<?php if ($task->isCompleted()): ?>
<strike><li><?= $task->description; ?></li></strike>
<?php else: ?>
<li><?= $task->description; ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</body>
</html>
output:
- go to store
go to market
- read book