Post your php programming tips here

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
Kyori
Forum Newbie
Posts: 23
Joined: Mon Oct 14, 2002 5:23 am
Contact:

Post your php programming tips here

Post by Kyori »

post helpful tips on proper php programming. Also put whether it's about security, querries, etc. May these be the newbies read first thread. thanks. I'm a new myself.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do not make mistakes :twisted:
Seriously, what kind of tips do you mean?

My first ones are: http://www.php.net/manual/en/ and www.google.com are your friends. Make intense use of them.

Maybe the problem is covered by a tutorial, maybe even at http://www.phpcomplete.com/ (that's the link at top of the page)

If your problem is of general nature ("how do I ...") it is likely that someone already asked before :arrow: search this forum or some more via phuse

Keep your code readable! note http://forums.devnetwork.net/viewtopic.php?t=3173. These styles differ not that much among one another - and that for some reason.....

Syntaxhighlighting editors can be of great help.

Code: Select all

<?php // a example-script
echo 'e.g. easier to find the wrong string-format';
echo 'when it's colored';
echo 'see?';
?>
is easier to read/debug than

Code: Select all

<?php // a example-script
echo 'e.g. easier to find the wrong string-format';
echo 'when it's colored';
echo 'see?';
?>
Take a look e.g. at http://forums.devnetwork.net/viewtopic.php?t=3495

When your project gets more complex and more complex errors occure you will appreciate the help a debugger offers. printf/echo-debugging sometimes may be feasable but a real debugger is better ;)
http://www.php.net/manual/en/debugger.php
DBG: Php Debugger
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

before you do any coding (esp. on a larger application) break out a notebook and a pencil and think through how your application is going to function, the resources it is going to need and what the output will look like. nothing beats outlining your project before hand, it helps to sort your thoughts before you bumble your code.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

X......

Post by BDKR »

before you do any coding (esp. on a larger application) break out a notebook and a pencil and think through how your application is going to function, the resources it is going to need and what the output will look like. nothing beats outlining your project before hand, it helps to sort your thoughts before you bumble your code.
MyDimension is 100% right. Nothing helps more than this.

Other than that, thinking outside of the box is alright. Allways be open to new ideas.

Cheers,
BDKR
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

I see a lot of programmers using \" in variables and echo functions to output the ". Most times this is used in html code, like

Code: Select all

echo("<font class="b">");
I find this unnecesary, I just use the '

Code: Select all

echo("<font class='b'>");
I've made it a habit while writting html code to always use the ' instead of ". It saves a character, and i think it looks better. So far I have not come across a browser that couldnt deal with a ' instead of a ".

And this is a good read. I'm a bad('coder'); too, so don't feel bad if you do these things.
http://www.evilwalrus.com/articles/read.php?aid=16
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

why not simply

Code: Select all

echo '&lt;font class="b"&gt;';
?
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

volka wrote:why not simply

Code: Select all

echo '&lt;font class="b"&gt;';
?
Yeah, you are right. It's that whole "learn something new every day". Way i figure it, if you use echo "... then you can use the ' as a quote in the string, and if you use echo '... then you can use ". After reading the evilwalrus article i posted the link for, I won't be using () any more.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

took me some time to get over that "beh, looks like shell-scripting" but now I'm (almost) always using echo string,string,string rather than print(string.string.string)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Lots more that can't even be covered here, but other major things to remember are :

Ask yoursefl :Will sensative information be secure if I send it this way?

Get some friends of yours that like to break into systems, and see if they can gain superuser rights through a vulnerability in your script

Use good error handling skills. If you think a portion of your script is buggy, or not working, try putting error handlings between the lines, and see which one generates the error. Easiest way to debugging.

Don't be afraid. PHP is very coder friendly, and very, VERY superior if you can master it.

Above all, view other people's scripts. Look at their problems they are having, how they resolved it, or how they wrote their code that does the same thing as yours, and find their strengths and weaknesses and variations.

Other then that, that's about the only thing I can give ya coming from a n00b 8) 8O :D [/b]
davro
Forum Newbie
Posts: 8
Joined: Fri Nov 01, 2002 6:54 am

Post by davro »

Don`t know if this is legal, but works for me use this quite a lot to comment out large areas of code.
comment, uncomment ect.

Code: Select all

///*

function test ()
&#123;
// blah code
&#125;

function comments ()
&#123;
// moreblah code
&#125;

//*/

// can eaily be commented out like so

/*

function test ()
&#123;
// blah code
&#125;

function comments ()
&#123;
// moreblah code
&#125;

//*/
Porter
Forum Newbie
Posts: 5
Joined: Thu Jul 04, 2002 1:30 am
Location: Clearwater, FL
Contact:

Post by Porter »

Remember to check the PHP Manual for functions for doing things that seem pretty simple to do. It'll save a ton of time.
Post Reply