A Silly way to comment in your Code

During my beginners year of writing code, i used to comment unnecessarily. I realised this after i saw the code written by senior developers. The main purpose of the comment is to make your code understood by other developers who are working together in the same project or for other contributors.

Let us take an example of the silly code:

<?php

// Assigning the value to firstNumber
firstNumber = 9

// Assigning the value to secondNumber
secondNumber = 12

// Creating variable to store the sum of firstNumber and secondNumber
sumOfNumbers = 0;

// Adding both firstNumber and secondNumber and storing it to sumOfNumbers
sumOfNumbers = firstNumber + secondNumber

// Displaying the result
echo sumOfNumbers;
?>

Read the code above, how do you feel about that?

The person who will read your code will be developer or some one who understands the code. If he does not understand that firstNumber = 9 is assigning number 9 to variable firstNumber, then he needs to learn programming. It is not your job to make him understand this.

However, this commenting style might be helpful if you are University Professor teaching how-to-write-code to your student.

So, the better version of the above code would be like this:

<?php
firstNumber = 9
secondNumber = 12
sumOfNumbers = 0;
sumOfNumbers = firstNumber + secondNumber
echo sumOfNumbers;
?>

Much better isn’t it?

So, unless you are teaching programming to someone beginners don’t write unnecessary comment in your code for two reasons. First one is it makes code unreadable to some sense. Next one is other programmers thinks you are silly because you think they are silly.

How to set up basic PHP Project structure with Composer

Most of the time beginners learning PHP are unaware of Composer, if you are among them, Composer is a dependency management tool for PHP.

Let’s take a simple example, rather than using include command in each of the PHP file, composer makes it easy by specifying that file as a dependency. Another example would be if you use some library then it makes easier to use and update it with a single command.

Continue reading How to set up basic PHP Project structure with Composer

How to Install PHP Composer in your Mac

Composer is the easiest way to manage dependencies to your small to large PHP projects.

Use the following commands to install composer in your Mac

$ curl -sS https://getcomposer.org/installer | php

Now the composer is installed in your current directory.

Now, you can access it using the following command

$ php composer.phar [command]

It works but you don’t always want to use this directory path to access the composer, So in the next step we will make it accessible globally.

Let us move it to usr/local/bin to make it globally accessible from your terminal.

$ mv composer.phar usr/local/bin

Next, create alias and add it to your bashrc file to load it everytime you restart your terminal or mac book.

$ echo 'alias composer="php /usr/local/bin/composer.phar"' >> ~/.bash_profile

That’s all, Restart your terminal and now try using composer in your terminal and it works. Every time the terminal opens it autoloads this alias now.

Bonus: If it is for other platform such as Ubuntu, you need to write to bashrc instead of bash_profile.

$ echo 'alias composer="php /usr/local/bin/composer.phar"' >> ~/.bashrc