Skip to content

Instantly share code, notes, and snippets.

@rhynodesigns
Created June 17, 2016 20:35
Show Gist options
  • Save rhynodesigns/a27ed0632c931695be20f6cf02ace109 to your computer and use it in GitHub Desktop.
Save rhynodesigns/a27ed0632c931695be20f6cf02ace109 to your computer and use it in GitHub Desktop.
UpstatePHP 101: Persistence
<?php
echo '<pre>';
echo '<br><br><br>';
$value = 10;
$value2 = '10';
var_dump($value == $value2); // true
echo '<br>';
var_dump($value === $value2); // false
echo '<h3>Value</h3>';
$value = 'YEA SUCKAS!';
echo '<h4>before</h4>';
var_dump($value);
file_put_contents('value.txt', $value);
echo '<h4>after</h4>';
$newValue = file_get_contents('value.txt');
var_dump($newValue);
echo '<br><br><br><br><br><br><br><br><br><br><br><br><br>';
echo '<br><br><br><br><br><br><hr><br><br><br><br><br><br>';
echo '<h3>Array</h3>';
$array = [
'name' => 'Red Beard',
'size' => 'Husky',
'float' => 444.22,
'int' => 40
];
echo '<h4>before</h4>';
var_dump($array);
file_put_contents('array.txt', $array);
echo '<h4>after</h4>';
$newArray = file_get_contents('array.txt');
var_dump($newArray);
echo '<br><br><br><br><br><br>';
echo '<h3>Array - Serialize</h3>';
echo '<h4>before</h4>';
var_dump($array);
var_dump(serialize($array));
file_put_contents('array.txt', serialize($array));
echo '<h4>after</h4>';
$newArray = unserialize(file_get_contents('array.txt'));
var_dump(file_get_contents('array.txt'));
var_dump($newArray);
var_dump($array === $newArray); //
echo '<br><br><br><br><br><br><br><br><br><br><br><br><br>';
echo '<br><br><br><br><br><br><hr><br><br><br><br><br><br>';
echo '<h3>Object</h3>';
$object = new StdClass();
$object->name = 'Big Drum';
$object->type = 'Awesome';
echo '<h4>before</h4>';
var_dump($object);
var_dump(serialize($object));
file_put_contents('object.txt', serialize($object));
echo '<h4>after</h4>';
$newObject = unserialize(file_get_contents('object.txt'));
var_dump(file_get_contents('object.txt'));
var_dump($newObject);
var_dump($object === $newObject); //
echo '<br><br><br><br><br><br>';
echo '<h3>Custom Object</h3>';
class Test
{
private $height;
private $width;
public function __construct()
{
$this->height = 10;
$this->width = 2;
}
}
$customObject = new Test;
echo '<h4>before</h4>';
var_dump($customObject);
file_put_contents('customObject.txt', serialize($customObject));
echo '<h4>after</h4>';
$newCustomObject = unserialize(file_get_contents('customObject.txt'));
var_dump($newCustomObject);
echo '<br><br><br><br><br><br><br><br><br><br><br><br><br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment