Page 1 of 1

Make long line of code more readable

Posted: Thu Jan 08, 2015 9:42 am
by cecilchampenois
I have a long line of code, a SQL statement to be exact that needs to be shortened down to be readable, but being new to PHP I am not sure how to do this:

Code: Select all

$sql = "SELECT client_no, access_level, logo_file, main_page, last_login, audit_year, active_client, email_address FROM $table_name WHERE login_id='$myusername' and login_password=aes_encrypt('$mypassword', 'bubbagump')";
This line of code extends way out to the right. Can I chop it up, such as:

Code: Select all

$sql = "SELECT client_no, access_level, logo_file, main_page, last_login, audit_year, active_client, " .
           "email_address FROM $table_name WHERE login_id='$myusername' and " . 
           "login_password=aes_encrypt('$mypassword', 'bubbagump')";
For some reason, I don't think that my chopping this up is a good thing.

Re: Make long line of code more readable

Posted: Thu Jan 08, 2015 9:50 am
by Christopher
cecilchampenois wrote: This line of code extends way out to the right. Can I chop it up, such as:

Code: Select all

$sql = "SELECT client_no, access_level, logo_file, main_page, last_login, audit_year, active_client, " .
           "email_address FROM $table_name WHERE login_id='$myusername' and " . 
           "login_password=aes_encrypt('$mypassword', 'bubbagump')";
For some reason, I don't think that my chopping this up is a good thing.
Formatting ("chopping up") is a fine thing. You don't even need to concatenate strings to do it. This works in PHP and is fine for things like SQL, XML, JSON, etc. where whitespace is ignored (though returns in strings can be problems elsewhere e.g., filenames, usernames/passwords, etc.):

Code: Select all

$sql = "SELECT client_no, access_level, logo_file, main_page, last_login, audit_year, active_client, email_address 
		FROM $table_name 
		WHERE login_id='$myusername' and login_password=aes_encrypt('$mypassword', 'bubbagump')";

Re: Make long line of code more readable

Posted: Thu Jan 08, 2015 10:05 am
by cecilchampenois
Christopher, that is just too simple!

Well, thank you so much for clearing that up for me!