Update php.md (#924)

* Update php.md

* Update php.md
This commit is contained in:
Jan Domański 2018-11-16 20:10:22 +00:00 committed by chad d
parent 898c8d904b
commit 658c88a8c3
1 changed files with 65 additions and 2 deletions

View File

@ -28,10 +28,20 @@ See: [PHP tags](http://php.net/manual/en/language.basic-syntax.phptags.php)
```php ```php
<?php <?php
$fruits = array( $fruitsArray = array(
"apple" => 20, "apple" => 20,
"banana" => 30 "banana" => 30
) );
echo $fruitsArray['banana'];
```
Or cast as object
```php
<?php
$fruitsObject = (object) $fruits;
echo $fruitsObject->banana;
``` ```
### Inspecting objects ### Inspecting objects
@ -44,3 +54,56 @@ var_dump($object)
Prints the contents of a variable for inspection. Prints the contents of a variable for inspection.
See: [var_dump](http://php.net/var_dump) See: [var_dump](http://php.net/var_dump)
### Classes
```php
class Person {
public $name = '';
}
$person = new Person();
$person->name = 'bob';
echo $person->name;
```
### Getters and setters
```php
class Person
{
public $name = '';
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
}
$person = new Person();
$person->setName('bob');
echo $person->getName();
```
### isset vs empty
```php
$options = [
'key' => 'value',
'blank' => '',
'nothing' => null,
];
var_dump(isset($options['key']), empty($options['key'])); // true, false
var_dump(isset($options['blank']), empty($options['blank'])); // true, true
var_dump(isset($options['nothing']), empty($options['nothing'])); // false, true
```