Home > PHP Scripts Examples and Tutorials > PHP coding tips to write less code (If then else and for loop)
India beat SL by four wickets
Sri Lanka using a mediator to release fishermen from pirates – Rajitha
Govt. has noted Nasheed’s resignation and VP’s appointment
Police to investigate ‘suspicious’ Wellawatta fire
Deportees from Togo to be checked for yellow fever
වැල්ලවත්ත ගින්න ගැන විශේෂ විමර්ශනයක්
Maldives president resigns amid protests
UNP also protest at Hulftsdorp
Indian Coast Guard nabs 5 Lankan fishing boats
SL wins toss
SF brought to court
Get well soon, SLC tells Yuvi
We need democracy plus; Ranil
White flag appeal postponed
UNP DNA join hands in protest
India still ‘world class’ insists Mathews
SL to market Ayurveda drugs
Nine hand grenades found near Aluth Kade Court
Hirunika to join active politics
IPL teams splash the cash for Mahela and Murali
Are you in Love?
10 Top Sex Secrets Men won't Tell
Top 10 — Female Turn Ons
10 Commandments for good wives
How to live a happy and satisfied Life
Did I marry the right person?
Are You Happy
Alcohol makes men BETTER in the bedroom, scientists claim
Too busy for Love Making?
Have you gone off sex?
Love Isn't Blind
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Are You Getting Attracted To Another Person?
Do you feel loved?
PHP coding tips to write less code (If then else and for loop)
PHP is a good language, but there are always surprises. This tip is useful to "lazy" developers who do not even think about variable names. They may prefer magic names like ${0} and 0 is good enough variable name, why not... So here are a few tips that can make your code shorter and harder to read :-)
1. Use || (or) and && (and) operations instead of if.
// A lot of code
$status = fwrite($h, 'some text');
if (!$status) {
log('Writing failed');
}
// Less code
${0} = fwrite($h, 'some text');
if (!${0}) log('Writing failed');
// Even less code
fwrite($h, 'some text') or log('Writing failed');
2. Use ternary operator.
// A lot of code
if ($age < 16) {
$message = 'Welcome!';
} else {
$message = 'You are too old!';
}
// Less code
$message = 'You are too old!';
if ($age < 16) {
$message = 'Welcome!';
}
// Even less code
$message = ($age < 16) ? 'Welcome!' : 'You are too old!';
3. Use for instead of while.
// A lot of code
$i = 0;
while ($i < 100) {
$source[] = $target[$i];
$i += 2;
}
// less code
for ($i = 0; $i < 100; $source[] = $target[$i+=2]);
4. In some cases PHP requires you to create a variable. Some examples you can find in PHP fluent API tips article. Another example is getting array element when array is returned by the function.
$ext = pathinfo('file.png')['extension'];
// result: Parse error: syntax error, unexpected '[' in ... on line ...
To handle all these situation you can create a set of small functions which shortcuts frequently used operations.
// returns reference to the created object
function &r($v) { return $v; }
// returns array offset
function &a(&$a, $i) { return $a[$i]; }
5. Explore the language you use. PHP is very powerful and has a lot of functions and interesting aspects of the language which can make your code more efficient and short.
6. When it is better to write more and then read the code easily, do not be lazy. Spend a few seconds and write a comment and more readable construction. This is the only tip in this list that really can save hours, not minutes.

http://www.alexatnet.com
Guidelines: You must register with a social media account such as Facebook, Twitter, Yahoo, etc. to comment on this story. Click on the "Login" button below to choose your login account of choice. We welcome your thoughts, but this is not an open forum. For the sake of all readers, please refrain from the use of obscenities, personal attacks or racial slurs. All comments must remain on topic and cyber bullying will not be tolerated. All comments are subject to our terms of service. Comments that do not comply may be removed. Repeat offenders will lose commenting privileges.
News Categories
Accidents and Incidents
Art and Society
Business and Finance
Education and Employment
Environment and Development
Help and Aid
Science and Technology
Security and Politics
Share Market and Exchange Rates
Sports and Entertainment
Tourism and Foreign Affairs
Women and Health
Information
Women and Health
Food Recipes
Love and Relationship
Web and IT
AJAX Examples and Tutorials
Java Scripts Examples and Tutorials
PHP Scripts Examples and Tutorials
Men want good-looking women
Making sex exciting
Duminda unmasked by Anarkali
Too busy for Love Making?
Srikanth Vandana got a Baby Boy-Father and Son Born on Same Day!
Anjali’s days with Bharathiraja and K. Balachandar
Have you gone off sex?
Sex and Shoes
Dirty Talk!
Happy Birthday Surya
Alcohol makes men BETTER in the bedroom, scientists claim
Sexless Marriages
Are You A Lazy Lover?
5 Secret Relationship Resolutions
Outsider Played Actress Sneha Hot Sexy Hip Cheap Photos?

































