MY .bash_profile for Laravel

I’ve only started using my .bash_profile a couple of years ago. To be honest I look back now and think, ‘What a plonker, you should have been using since day one’

Why? If you are even a little bit lazy it can save you loads of typing. If you are not lazy it can help you get more done in less time.

For example. Instead of typing

php artisan tinker

I can type

tinker

Still not convinced? How about opening the Apache error log, scroll to the last entry and then automatically scroll down when a new error has been reported?

This

tail -f /usr/local/var/log/apache2/error_log | sed "s/\\\n/^M^L/g"

Becomes this

apachelog

Here is my Lavavel and Lithium .bash_profile, it is not my complete profile but is enough to give you a taste.

#List all including .hidden files

alias ll='ls -la'

#Apache log 

alias apachelog='tail -f /usr/local/var/log/apache2/error_log | sed "s/\\\n/^M^L/g"'

#alias apachelog='tail -f /usr/local/var/log/apache2/error_log | sed "s/\\\n/" | sed "s/\\\n/^M^L/g"'

#PHPSpec

alias phpspec='vendor/bin/phpspec'

#Apache http.conf

alias apacheconfig='open -e /usr/local/etc/apache2/2.4/httpd.conf'

#Apache status

alais apachestatus='ps -aef | grep httpd'

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

export PATH="~/.composer/vendor/bin:$PATH"

#PHP Artisan

alias art='php artisan'

#Laravel PHP  artisan tinker

alias tinker='php artisan tinker'

#Lithium Terminal/console

alias li3='libraries/lithium/console/li3'

#Start Apache and MySQL

function startme(){

        sudo apachectl -k restart

        mysql.sever start

        pwd 

} 

PHP for loop

The PHP ‘for loop’ based on my experience is an underused PHP function. We all have seen the following…

<?php
$array = [  ];// Of things

for($i=1; $i <= count($array); $i++)
{
// Do something 
}
?>

This is what happens

  1. Initialise the loop with the variable $i set to 1
  2. Count how many items are in the array and compare with $i to see if it greater or less
  3. Do something
  4. Add 1 (increment) to $i
  5. Count how many items are in the array and compare with $i to see if it greater or less
  6. Do something
  7. Add 1 (increment) to $i

Repeat

And this works fine. Lets look at the structure of the ‘for loop’ and see what is happening.

The For Loop Structure

for( {exp1}; {exp2}; {exp3} )
{
//Do something
}
  • Exp1 will be run once only and at the first time around the loop ( AKA an iteration)
  • Exp2 Will run at the beginning of every iteration ( or go around the loop)
  • Exp3 Will run at the end of every iteration.

In the original  code if the array is large it can have a negative impact on performance. Can it be made better?

Yes. How about the following?

<?php
$array = [  ];// Of things
$array_count = count($array);
for($i=1; $i <= $array_count; $i++)
{
// Do something 
}
?>

This works much better. The array is only counted once, but in my opinion it does not read well and feels clunky. What can be done?

The ‘for loop’ has 3 blocks (exp1, exp2, exp3) each separated with a colon. What you put in these blocks is upto you, you can even leave them blank. That being said lets rewrite the original code so that

  1. It works as expected
  2. It does so efficiently
  3. When you read it, it makes clearer sense;
<?php
$array = [  ];// Of things
for($i=1, $array_count = count($array); $i <= $array_count; $i++)
{
// Do something 
}
?>

Is that not better?

See PHP.net for more