Make long line of code more readable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cecilchampenois
Forum Commoner
Posts: 47
Joined: Thu Nov 06, 2014 10:29 am
Location: Gilbert, Arizona
Contact:

Make long line of code more readable

Post 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.
Cecil Champenois
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Make long line of code more readable

Post 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')";
(#10850)
cecilchampenois
Forum Commoner
Posts: 47
Joined: Thu Nov 06, 2014 10:29 am
Location: Gilbert, Arizona
Contact:

Re: Make long line of code more readable

Post by cecilchampenois »

Christopher, that is just too simple!

Well, thank you so much for clearing that up for me!
Cecil Champenois
Post Reply