Jump to content
xisto Community
Sign in to follow this  
Silver Bluewater

Little Tips For Php Coder

Recommended Posts

When you're coding, you happen to get confused with these features although you may not.

 

"content" <- This is used for including variables with the set of characters

 

'content ' <- This is used for including every characters as is.

 

These may be looking too simple tip. I know that.

 

The matter is this can be really useful for most of the times while you're coding in PHP.

 

Try it out when you were not aware of these features.

 

You will like it for sure.

 

 

--

Have a nice day!

 

My blog : silverbluewater.blogspot.com

Edited by Silver Bluewater (see edit history)

Share this post


Link to post
Share on other sites

I am also adding little tips. This is very basic that every php sentence will be ended by a ";". Most beginners make the mistake. They forgot to add a ; sign at the end of the sentence. Another common mistake to the beginner is not to add closing " sign when they use starting " sign. Make me clearer

echo "This is a tutorial";

This is the valid code, but the common mistakes are not to add closing " sing or ; sign. eg.

echo "This is a tutorial;

or

echo "This is a tutorial"

And I also found that few beginners also forget to add "?>" at the end of the php code. They add the starting "<?php" but forget to close it bye adding "?>".So, the beginners be little careful about the 3 common mistake1. Omitting closing " sign.Common mistake

echo "This is a tutorial;

Correct code

echo "This is a tutorial";

2. Omitting ; signCommon mistake

echo "This is a tutorial"

Correct code

echo "This is a tutorial";

3. Omitting "?>"Common mistake

<?phpecho "This is a tutorial";

Correct code

<?phpecho "This is a tutorial";?>

Hope these little tips will help the beginner of php coding.

Share this post


Link to post
Share on other sites

in fact, closing the php code, if it's only a PHP file isn't necessary, some programmers even don't close it, to not get headers already sent error, in practice, it's even better to not close the php code with ?> but personally I usually do it, due to I see it to be more clean, but you wonder why it could be a bad idea to close the file?

Sometimes, when closing the php tag ?> you can push enter and leave an empty new line, but php will interpreted it like an output and will output the newline, so sometimes, if you later will decide to use sessions, cookies or headers and etc. you may be getting the Headers already sent error and output was sent there and there, you may have a headache, due to you'll be browsing the file and you won't find any output and usually you won't notice anything in the beginning of the file, the new line or tab, you might think the error message is creating the new line, so this is why in practice if it's only a php file with classes or functions and doesn't output anything, it isn't necessary to close it, it's not even an error, but IT IS an ERROR when for example your doing this:

<?php echo "Hello World";<html><head></head><body></body></html>

It's really a bad mistake, because PHP will try to execute the HTML content so you should close the tag. :(

Share this post


Link to post
Share on other sites

A fantastic piece of advice (in my opinion) for PHP coders - and coding in general - is the joy that is the indent. Tabbing is my generally preferred method, meaning instead of:

 

if (foo != bar){while (foo < bar){foo++;}}else{if (foo == bar*2){foo = 0;}}
Which just looks nasty, and is an absolute nightmare to debug, you get this:

 

if (foo != bar){	while (foo < bar)	{		foo++;	}}else{	if (foo == bar*2)	{		foo = 0;	}}
Which is far more readable. It's most likely been mentioned in a whole host of other topics, but never underestimate the advantages of good indentation. Some people use spaces instead of tabs, which works pretty well, but I find tabs just look neater (especially as I use Notepad++, which makes grouping this with curly braces a lot clearer). Getting your HTML code to look nice with the use of the echo function is generally a little trickier, although a little more irrelevant, as you'll find that the level of indentation your PHP code is at will most likely not be the same one for HTML code at that point. You can get around this by either ignoring it (as the browser does anyway) or by working out the level of indentation it should be at and tabbing the echo statements in as far as they need to be. This can get a little messy in the PHP itself (as you have two lots of indentation going on at once), but does make the HTML look decidedly more organised.

 

Either way, the point I'm trying to get across is that 'indentation is generally a good idea'.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.