Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Little Tips For Php Coder
Silver Bluewater
post Jun 12 2007, 03:17 AM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 21
Joined: 28-May 07
Member No.: 22,208



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

This post has been edited by Silver Bluewater: Jun 12 2007, 03:54 AM
Go to the top of the page
 
+Quote Post
BDIT
post May 1 2008, 06:18 PM
Post #2


Newbie [ Level 2 ]
Group Icon

Group: [HOSTED]
Posts: 11
Joined: 30-April 08
Member No.: 30,066



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

CODE
echo "This is a tutorial";


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

CODE
echo "This is a tutorial;

or
CODE
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 mistake

1. Omitting closing " sign.
Common mistake
CODE
echo "This is a tutorial;

Correct code
CODE
echo "This is a tutorial";


2. Omitting ; sign
Common mistake
CODE
echo "This is a tutorial"

Correct code
CODE
echo "This is a tutorial";


3. Omitting "?>"
Common mistake
CODE
<?php
echo "This is a tutorial";

Correct code
CODE
<?php
echo "This is a tutorial";
?>


Hope these little tips will help the beginner of php coding.
Go to the top of the page
 
+Quote Post
Quatrux
post May 2 2008, 03:57 PM
Post #3


the Q
Group Icon

Group: [HOSTED]
Posts: 1,022
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



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:

CODE
<?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. smile.gif
Go to the top of the page
 
+Quote Post
Mordent
post May 2 2008, 05:59 PM
Post #4


Premium Member
Group Icon

Group: [HOSTED]
Posts: 223
Joined: 30-June 07
Member No.: 23,045



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:

CODE
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:

CODE
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'.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Secure Php Coding(10)


 



- Lo-Fi Version Time is now: 5th September 2008 - 11:07 AM