Skip to content

Commit

Permalink
Unified conditional arguments and replaced APC example
Browse files Browse the repository at this point in the history
  • Loading branch information
telephone committed Aug 14, 2012
1 parent 876ec75 commit 4f086c3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion _posts/03-05-01-Command-Line-Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Let's write a simple "Hello, $name" CLI program. To try it out, create a file na

{% highlight php %}
<?php
if($argc != 2) {
if ($argc != 2) {
echo "Usage: php hello.php [name].\n";
exit(1);
}
Expand Down
13 changes: 6 additions & 7 deletions _posts/05-03-01-Date-and-Time.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ output.
$raw = '22. 11. 1968';
$start = \DateTime::createFromFormat('d. m. Y', $raw);
echo "Start date: " . $start->format('m/d/Y') . "\n";
echo 'Start date: ' . $start->format('m/d/Y') . "\n";
{% endhighlight %}

Calculating with DateTime is possible with the DateInterval class. DateTime has methods like `add()` and `sub()` that
Expand All @@ -30,29 +30,28 @@ $end = clone $start;
$end->add(new \DateInterval('P1M6D'));

$diff = $end->diff($start);
echo "Difference: " . $diff->format('%m month, %d days (total: %a days)') . "\n";
echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . "\n";
// Difference: 1 month, 6 days (total: 37 days)
{% endhighlight %}

On DateTime objects you can use standard comparison:
{% highlight php %}
<?php
if($start < $end) {
if ($start < $end) {
echo "Start is before end!\n";
}
{% endhighlight %}
One last example to demonstrate the DatePeriod class. It is used to iterate over recurring events. It can take two
DateTime objects, start and end, and the interval for which it will return all events in between.
{% highlight php %}
<?php
// output all thursdays between $start and $end
$periodInterval = \DateInterval::createFromDateString('first thursday');
$periodIterator = new \DatePeriod($start, $periodInterval, $end, \DatePeriod::EXCLUDE_START_DATE);
foreach($periodIterator as $date)
{
foreach ($periodIterator as $date) {
// output each date in the period
echo $date->format('m/d/Y') . " ";
echo $date->format('m/d/Y') . ' ';
}
{% endhighlight %}

Expand Down
11 changes: 4 additions & 7 deletions _posts/10-03-01-Object-Caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ Example logic using APC:
{% highlight php %}
<?php
// check if there is data saved as 'expensive_data' in cache
$data = apc_fetch('expensive_data');
if (!$data)
{
// data not in cache, do expensive call and save for later use
$data = get_expensive_data();
apc_store('expensive_data', $data);
if (apc_fetch('expensive_data') === false) {
// data is not in cache; save expensive call for later use
apc_add('expensive_data', get_expensive_data());
}
print_r($data);
print_r(apc_fetch('expensive_data'));
{% endhighlight %}
Learn more about popular object caching systems:
Expand Down

0 comments on commit 4f086c3

Please sign in to comment.