mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-09 15:36:36 +02:00
Unified conditional arguments and replaced APC example
This commit is contained in:
@@ -22,7 +22,7 @@ Let's write a simple "Hello, $name" CLI program. To try it out, create a file na
|
|||||||
|
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
if($argc != 2) {
|
if ($argc != 2) {
|
||||||
echo "Usage: php hello.php [name].\n";
|
echo "Usage: php hello.php [name].\n";
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,7 @@ output.
|
|||||||
$raw = '22. 11. 1968';
|
$raw = '22. 11. 1968';
|
||||||
$start = \DateTime::createFromFormat('d. m. Y', $raw);
|
$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 %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Calculating with DateTime is possible with the DateInterval class. DateTime has methods like `add()` and `sub()` that
|
Calculating with DateTime is possible with the DateInterval class. DateTime has methods like `add()` and `sub()` that
|
||||||
@@ -30,18 +30,18 @@ $end = clone $start;
|
|||||||
$end->add(new \DateInterval('P1M6D'));
|
$end->add(new \DateInterval('P1M6D'));
|
||||||
|
|
||||||
$diff = $end->diff($start);
|
$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)
|
// Difference: 1 month, 6 days (total: 37 days)
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
On DateTime objects you can use standard comparison:
|
On DateTime objects you can use standard comparison:
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
if($start < $end) {
|
if ($start < $end) {
|
||||||
echo "Start is before end!\n";
|
echo "Start is before end!\n";
|
||||||
}
|
}
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
One last example to demonstrate the DatePeriod class. It is used to iterate over recurring events. It can take two
|
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.
|
DateTime objects, start and end, and the interval for which it will return all events in between.
|
||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
@@ -49,10 +49,9 @@ DateTime objects, start and end, and the interval for which it will return all e
|
|||||||
// output all thursdays between $start and $end
|
// output all thursdays between $start and $end
|
||||||
$periodInterval = \DateInterval::createFromDateString('first thursday');
|
$periodInterval = \DateInterval::createFromDateString('first thursday');
|
||||||
$periodIterator = new \DatePeriod($start, $periodInterval, $end, \DatePeriod::EXCLUDE_START_DATE);
|
$periodIterator = new \DatePeriod($start, $periodInterval, $end, \DatePeriod::EXCLUDE_START_DATE);
|
||||||
foreach($periodIterator as $date)
|
foreach ($periodIterator as $date) {
|
||||||
{
|
|
||||||
// output each date in the period
|
// output each date in the period
|
||||||
echo $date->format('m/d/Y') . " ";
|
echo $date->format('m/d/Y') . ' ';
|
||||||
}
|
}
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
|
@@ -28,15 +28,12 @@ Example logic using APC:
|
|||||||
{% highlight php %}
|
{% highlight php %}
|
||||||
<?php
|
<?php
|
||||||
// check if there is data saved as 'expensive_data' in cache
|
// check if there is data saved as 'expensive_data' in cache
|
||||||
$data = apc_fetch('expensive_data');
|
if (apc_fetch('expensive_data') === false) {
|
||||||
if (!$data)
|
// data is not in cache; save expensive call for later use
|
||||||
{
|
apc_add('expensive_data', get_expensive_data());
|
||||||
// data not in cache, do expensive call and save for later use
|
|
||||||
$data = get_expensive_data();
|
|
||||||
apc_store('expensive_data', $data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
print_r($data);
|
print_r(apc_fetch('expensive_data'));
|
||||||
{% endhighlight %}
|
{% endhighlight %}
|
||||||
|
|
||||||
Learn more about popular object caching systems:
|
Learn more about popular object caching systems:
|
||||||
|
Reference in New Issue
Block a user