What they never tell you about GitHub

GitHub is brilliant…. There are loads of tutorials, video and articles out there, recently I’ve spent time filling gaps in my Git version control knowledge and something struck me as odd. Every tutorial should make it clear right from the start and reminders throughout their presentations that you

DO NOT STORE PASSWORDS, KEYS, USERNAMES, CREDENTIALS, ETC. ON GIT HUB

Why?

Because there are bastards out there who are scanning git repositories for anything that could help access secured areas or consume resources they should not.

Git does scan your repo’s and will warn you but its not in real time so be warned. Still not convinced? The image is of a free Amazon Web Service account that was set up as a learning exercise the keys where saved in a Git Repo and over the course of 3 days a free account had racked up nearly $10,000US .

What can you do, if you accidentally commit a password, key, etc? Delete the branch? Roll back?

No!

The best advice and this is also given by Git themselves is to consider your data compromised and your accounts that use the data hacked. Once again, rolling back and deleting does not work.

So reset your passwords/key etc and don’t just add a 1 at the end or swap out vowels for 12345.

How do you manage password and environment values?

Have a Google for git .gitignore or visit dotIgnore on GitHub

strpos() not working?

PHP strpos() not working as expected? It could be a  “non-strict” comparison problem.

When using strpos() to determine whether a substring exists within a string the  results can be misleading: Remember FALSE == 0?

Consider the following:

$quote = 'Dave rocks';
 
if (strpos($quote, 'Dave')) {
    echo 'Dave is awesome.';
} else {
    echo 'Dave is not awesome.';
}

strpos() Returns position is 0 ( zero ) that is evaluated as FALSE so, “Dave is not awesome”.

Much better. In this case adding the strict comparison === ( 3 equals ) to the “if” statement asks if strpos() returns a number and  is not strictly FALSE. So, “Dave is awesome”

$quote = 'Dave rocks';
 
if (strpos($quote, 'Dave') !== FALSE) {
    echo 'Dave is awesome.';
} else {
    echo 'Dave is not awesome.';
}

For more see PHP.net

 

 

 

Laravel Pagination

Pagination with Laravel (5.4 at the time of writing) is very easy.

Say you want a table of users

In your controller:

$user = User::all()->paginate(10);

In the view loop through your users as normal and then add

$user->links() where ever you feel like it and more than one place if you are feeling dandy.

There is more..

But say you want more than one paginate-able list/table on the same page? This is the paginators signature

paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

Easy, extending the original query lets get all the threads/comments for a user. Assuming your User model can dish out Threads and Comments

$user = User::find(1);
$threads = $user->threads()->paginate(4, ['*'], 'threads');
$comments = $user->comments()->paginate(4, ['*'], 'comments');

Then in your view loop through your comments and add

$comments->links();

Loop through your threads and add

$threads->links();

 

 

 

PHP Don’t Abbreviate

Simply put, please do not abbreviate in your code.

Always code as if the guy who ends up maintaining your code will be a
violent psychopath who knows where you live.  Code for readability. John F Wood

If others have to read it they will have no idea what an abbreviation means and when you come back to it in 6 months time will you remember what this does?

<?php
function delHisFile($p) { 
// Do something
 }

Or

function sort(array $a) {
 for ($i = 0, $c = count($a);  $i < $c;  $i++) { 
// Do something
 }
}

How does this read?

<?php
function deleteHistoryFiles($path) { 
// Do something
 }


function sort(array $array) {
 for ($index = 0, $array_count = count($array); $index < $array_count;  $index++) { 
// Do something
 }
}

When I started out coding for some reason I thought variables, functions, methods and class names that where long and descriptive would have an adverse effect on performance ( I know daft eh) .

PHP foreach() function

Okay, another everyday snippet of PHP code tweaked.

We all use the following

<?php

foreach($array as $item){

// Do something

}

?>

Which works fine, sometimes the // Do something gets complicated with HTML

<?php

foreach($array as $item){

echo '<tr>';

echo '<td>'.$item->first_name.'</td>';

echo '<td>'.$item->last_name.'</td>';

echo '<td><a href="'. $item->name .'">'.$item->email.'</a></td>';

echo '</tr>';

}

?>

After a while it gets messy and can be easy to have errors in your HTML which your editor might not pick up.

How about this for a solution – its an Alternative Form

<?php

foreach($array as $item) : ?>

<tr>

<td><?= $item->first_name ?></td>

<td><?= $item->last_name ?></td>

<td><a href="<?= $item->email ?>"><?= $item->email ?></a></td>

</tr>

<?php endforeach ?>

There are a few other ways but this reads really well. If you are looking to store the HTML as a variable before echoing it out have a look at ob_start(). However, creating chunks of HTML and passing them from place to another isn’t really good form – see separations of concerns  and MVC

 

This also works for if() statments

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