Page 2 of 2 FirstFirst 12
Results 41 to 73 of 73
  1. #41
    Join Date
    Mar 2006
    Location
    Newcastle Australia
    Posts
    0
    A great and useful "How-to", I learned a number of new things.

    Why not collect it all in one file and put a link to it on your web-site.

    Then people could get all the goodies without a lot of cutting and pasting.
      0 Not allowed!

  2. #42
    Join Date
    Mar 2006
    Location
    Canada
    Posts
    0
    I have to agree,... not boring at all!
    Nice work!
      0 Not allowed!

  3. #43
    Thanks a lot for your tips and tutorial.
    I have done simple shop and only one page just not totally functioning.After the checkout.Customers fill the required fields and the last page they will send what they have bought.I have changed it for a now as a formular and it comes to my email but has so much code and &&& symbos etc. Actuall it does not send to customers email too.
    Could you please help for rebuilding this order.php . Checkout etc functions but just the last actions for order.php does not function. It does not send the photo etc.what they have .I have made test order and comes 5 pages code to my email.

    Greetings
      0 Not allowed!

  4. #44
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Posts
    194
    Quote Originally Posted by $_patch
    sometimes, we need to have some randomized values. I just create my own random function to something like this

    toss($from,$to)

    inside toss is a code that will generate a random number between $from and $to...

    so if i need three values, toss(1,3) will return any value from 1 to 3.
    does this mean you make a function to randomly return 1 2 or 3?

    Haha, you could just use $rand = rand(1, 3);
    █ Software developer, web designer, and general clever-clogs.
    █ C#, XHTML, CSS, PHP, MySQL, MsSQL, T-SQL, Photoshop & more
      0 Not allowed!

  5. #45
    does this mean you make a function to randomly return 1 2 or 3?

    Haha, you could just use $rand = rand(1, 3);
    Or just $rand = rand(3); :-)
    Website Hosting by Rackset
    Professional, Affordable, Reliable

    Web Hosting Provider
      0 Not allowed!

  6. #46
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Okay, maybe I should correct some false statements here.

    * Commas are apaprently faster than periods when it comes to concatenation
    Commas and periods are two very different things. There is only one concatenation operator for PHP, and that is the period (.).

    For example, you cannot do this:

    PHP Code:
    $string 'foo','bar'# !!! Syntax error!! 
    I think where the confusion comes in is when people write code like this:

    PHP Code:
    $string 'foo';
    echo 
    $string,'bar'
    The reason the above code doesn't error out is because echo is a language construct that takes arguments as a comma separated list. Perhaps its more clear with this example:

    PHP Code:
    echo ($string,'bar'); 
    Which is equivalent to the one posted above. So please, don't think PHP has two concatenation operators -- it doesn't. There is only one. Also, goes without saying, one is not faster than the other.

    * When using comparison operators, you can either use == or === The latter will check for the type as well as the value and it is faster.
    === is not faster than ==. Please, tell me where you found this out

    * switch/case is faster than an if condition
    ?? How did you figure this one out?

    * Type cast any foreach arguments to avoid error messages
    This is very bad advice because it will lead to sloppy programming; mainly because PHP won't complain and will turn anything into an array. Instead, use the tools that are provided to you by PHP, namely is_array(). Your example can be better written as:

    PHP Code:
    $result someFunction();
    if (
    is_array($result))
    {
       foreach(
    $result as $foo => $bar)
       {
         
    /* ... */
       
    }

    * If you prepend a function with an AT sign (@), it will not spit out any errors. The same applies to user defined functions.
    This is true, but don't use this as an excuse to avoid checking for errors in your code. In my experience, @ creates more problems then it fixes because people don't know how to use it properly.

    I haven't had time to read the rest -- but these stood out.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise
      0 Not allowed!

  7. #47
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Posts
    194
    Quote Originally Posted by RACKSET
    Or just $rand = rand(3); :-)
    Hehe, nope.

    Warning: rand() expects exactly 2 parameters, 1 given in /home/imdsm/public_html/dev/test.php on line 3

    You must remember that the way rand works, is it takes a value from between "min" and "max":

    int rand ( [int min, int max] )


    You can view more information about this at http://uk2.php.net/rand


    █ Software developer, web designer, and general clever-clogs.
    █ C#, XHTML, CSS, PHP, MySQL, MsSQL, T-SQL, Photoshop & more
      0 Not allowed!

  8. #48
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Posts
    194
    Quote Originally Posted by fyrestrtr
    This is true, but don't use this as an excuse to avoid checking for errors in your code. In my experience, @ creates more problems then it fixes because people don't know how to use it properly.
    I agree with you on the above, but would like to add that @ is supposed to be used for functions which are liable to error when there isn't a bug. For example mysql_num_rows(), on some older php versions, will error if there weren't any rows selected by a query. This is a great example of how to properly use the @ prefix:

    PHP Code:
    <?php

    $result 
    mysql_query("select * from table");
    $numrows = @mysql_num_rows($result);

    /* or a better way to do this, would be to use type casting. */

    $result mysql_query("select * from table");
    $numrows = (int)@mysql_num_rows($result);

    /* in this case, if there aren't any rows returned, php wont error but instead return 0 instead of false. */

    ?>

    Hope this is of some use to you.
    █ Software developer, web designer, and general clever-clogs.
    █ C#, XHTML, CSS, PHP, MySQL, MsSQL, T-SQL, Photoshop & more
      0 Not allowed!

  9. #49
    A lot has been said, and a lot has been corrected, there are still some small things I'd like to point out tho...

    1. Heredoc
    PHP Code:
    <?php
    // Showing a huge chunk of HTML at a time //
    echo<<<END
    <font face="Verdana" color="Orange" size="3">Large, Orange Text in Font Size 3</font>
    <br><br>
    More HTML down here..
    <br><br>
    <div align="Center">Centered text</div>
    END;
    ?>
    Using the heredoc technique as shown above is best to be avoided. It's a lot slower then going in and out of php tags. The reason for this is that php will ignore parts of the file that are not between php tags and since pure html is of no interrest to php you can skip this and make php parse the page faster.

    2. Variables
    1. Use variable names that make sense.

    a) $string is a string, and $string_array is an array for example. $flag is a flag...
    Even better would be to use the following technique to write variables:
    PHP Code:
    // boolean
    $bIsActive FALSE;
    $bNeedsRefresh TRUE;

    // integer
    $iPhoneNumber 042323232;
    $iRandNum rand(3,6);

    // string
    $sErrorMsg 'This is an error';
    $sUserName 'JeanM';

    // query
    $qUserInfo mysql_query("SELECT * FROM user WHERE user = '".$sUserName."'");

    // array
    $aSmilies = array(':D',':(',':P');
    $aUserInfo mysql_fetch_assoc($qUserInfo); 
    To sum it up:
    1. Use the first letter to tell what kind of variable we are dealing with $un -> $sun
    2. Don't use short variables like $sun but instead write full names $sun -> $susername
    3. Use a case letter whenever a new word starts, this makes it easier to read: $susername -> $sUserName

    This makes coding and using variables A LOT easier and it really isn't more work. Yes you will get longer variables but who cares, what matters most is that you still understand your script 6 months after you first created the script.

    3. Quotation
    Using Single Quotes VS. Duoble Quotes
    some poeple don't know the difference between single and double quotes..
    single quotes are faster than double ones because it wont parse variables while the double quotes will look for variables to parse ..
    so if you have only hardcoded text, use single quotes but if you have variables use duoble quotes..
    example:
    PHP Code:
    $var 'value';

    echo 
    'this is $var'//prints "this is $var"
    echo "this is $var"//prints "this is value" 
    While the comment about single quotes being faster than double quotes is true, it is in general good practice to use only single quotes if possible.
    PHP Code:
    // parsing a string
    echo 'using single quotes is faster';

    // string with a variable
    $sPossible 'possible';
    echo 
    "This would be $sPossible";

    $sMuchBetter 'much better';
    echo 
    'But this would be '.$sMuchBetter
    Not only does this make php again parse the page faster (it doesn't have to check each character between double quotes to see if there is a variable present) but it's also a lot cleaner for yourself. For example, you no longer have to worry about using double quotes inside double quotes and, when using a proper php editor as noted above, it colors the variables in the correct way, if you had the variables between double quotes it would just parse them in red, as shown above.

    One thing to watch out for (which as been stated before in this thread) is that using linebreaks like \n or \r is not possible within single quotes, in this case you could to the following:
    PHP Code:
    echo "Text with a line break\n" 
    But even better would be
    PHP Code:
    echo 'Text with a line break'."\n" 
    3. queries: ` ` vs ' ' vs .. nothing
    This is incorrect, INSERT INTO table ($keys) VALUES ($values) ... $keys must be ` ` and $values must be ' '.
    example, `id`,`name` .
    It is best practice to never use backquotes but instead use column names that are easy to understand and at the same time arent too "general". the reason backquotes are used is to avoid getting an error when for example using the following query:

    PHP Code:
    mysql_query("SELECT * FROM users ORDER BY order"); 
    This will ouput an error because the column name "order" is reserved by mysql (see: http://dev.mysql.com/doc/refman/5.0/...ed-words.html). Using `` allows you to still use these words, but using backquotes in every query you make is just not feasable, just stick to using column names that have a prefix or use names that you are sure of are not used my mysql.
    Last edited by JeanM; 07-02-2006 at 09:43 AM.
      0 Not allowed!

  10. #50
    JeanM,

    I agree with what you said, with one exception. The microsoft-like-way of being annoyingly obnoxious with variable names, table names, query names, etc..

    $myhouse = 'nice';
    $sMyHouse 'nice';

    There is no purpose to this whatsoever.

    Similarly, in access (offtopic but I think thats where this mess started), you see things like:

    tblPerson
    qFindPeople
    sString

    Its silly. As if I didnt know tblPerson was a table, or that qFindPeople was a query. I think you should give variables / objects / etc sensible names, but prefixing them with characters that are supposed to show their purpose is meaningless, redundant, and obnoxious for the 'rest of us'.

    My other comment on HEREDOC... I love HEREDOC, because I dont have to worry about quotation rules. I just punch out what I want to put in.. I use it a lot for queries - I dont use it to output html / text.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"
      0 Not allowed!

  11. #51
    I guess it's a matter of preference, but I think that if you work with scripts that include a douzen pages and thousands of lines it's easy to loose track of what variable does what, and with clear naming and stating the type of variable that becomes more clear. For me the capitalized first letters make it easy to read the variables on a page when scanning trough a script that needs to be altered.

    Also the heredoc can indeed be used for that purpose, again it's a matter of preference.
      0 Not allowed!

  12. #52
    Join Date
    Jan 2005
    Location
    Leeds, England
    Posts
    183
    i use dreamweaver to code my PHP. it has colour highlighting automatic indentation. and has a built in ftp. i always upload my entire site to a test folder before showing it publicly.
      0 Not allowed!

  13. #53
    Very Helpful, Thanks
      0 Not allowed!

  14. #54
    thanks for the sweet post, because of this ive realised loads of mistakes in my programming and have sorted them out!! thanks once again
      0 Not allowed!

  15. #55
    very helpful. Thank you all for all the good tips.
    One thing I would like to know though..... with sessionstart, how can I have it where it is not in the first thing in the page, I would like to include it in a header but always got errors. any ideas would be great
    Web Hosting Review - Real Reviews by Real People Submit your company and get a link to your site, Read Reviews GET YOUR Voice HEARD and REVIEW your HOST NOW!!! Check out our IT Blog Tips & Tricks
      0 Not allowed!

  16. #56
    Join Date
    Feb 2006
    Location
    Buffalo NY
    Posts
    1,330
    I think you should mention use of variables, don't declare them unless you're going to use it a few times throughout your script.

    Also protecting yourself from SQL injections, when / should you cache your work, memcached, etc..

    I recommend new coders read these Zend tutorial(s) (There are three parts) for a good primer.

    Aside from that nice little writeup, easy to read and informative.

    Also I would usually use the following for a include.

    PHP Code:
    <?php
       
    include_once('config.php') or die('Error!');
    ?>
    *Note*
    Keep in mind the difference between include(), include_once(), require_once(), etc.
    Cody R.
    Hawk Host Inc. Proudly Serving websites since 2004.
      0 Not allowed!

  17. #57
    Join Date
    Nov 2007
    Location
    England
    Posts
    8
    Very nice, Especially for PHP learners
      0 Not allowed!

  18. #58
    Join Date
    Feb 2007
    Posts
    184
    Make sure you optimize your database structure and use indexes. Cache data in memory as much as possible, however this would only be useful if you're website is very inversive with queries. If it is an average site then use a opt cacher, such as XCache. Preform a crap load of benchmarks to help you eliminate most of the bottlenecks. Google can find you a lot of good tips.
      0 Not allowed!

  19. #59
    Combining lines is not necessarily a good idea. Some people get annoyed reading scripts containing combined lines.
      0 Not allowed!

  20. #60
    Programming is all about making your own style. Some people use all the PHP rules others just put something together and don't care. Still nice tutorial for people that just began programming PHP
      0 Not allowed!

  21. #61
    I agree, however everyone has their own style of programming.
      0 Not allowed!

  22. #62
    Very nice post, i like how good people are really dedicated to their craft.
      0 Not allowed!

  23. #63
    Nice stuff.It's for beginners.

    After working hours and hours with programming and php your biggest problem will be some stupid mistake you made And you will spend next whole day trying to find out what's wrong, then you will give up.Next day in the morning while you are drinking coffee - bang, you nailed the error.The problem was YOU.It's always your mistake
      0 Not allowed!

  24. #64
    Join Date
    Mar 2010
    Posts
    84
    Thanks man this tutorial was a little over my head, do you know of any more basic tutorials out there I can start with?
      0 Not allowed!

  25. #65
    Join Date
    Sep 2004
    Location
    Cluj-Napoca, Romania
    Posts
    460
    Move to Python and Django! Sorry, just joking .

    I think the best advice is to use a PHP Framework, like Zend.
    FLEIO - OpenStack billing system and control panel for service providers

      0 Not allowed!

  26. #66
    Frameworks do help a lot. They have power and flexibility. As well as giving you the ability to replicate things fairly easily for additional sites. Zend is a good one.
      0 Not allowed!

  27. #67
    Join Date
    Aug 2010
    Location
    Somerset, UK
    Posts
    53
    Quote Originally Posted by giropets View Post
    5 - Indenting Your PHP Codes
    I don't personally like to indent my PHP codes, but it helps when reading it. When I do have to, I use the tab key to accomplish this. Example :
    PHP Code:
    <?php
    // Settings //
        
    $var1 "This";

    // Showing Variables //
        
    if($var1 == "This"){
            echo
    "You said This";
        }else{
            echo
    "You said That";
        }
    ?>
    I would add that a lot of people I know tend to use line breaks to seperate blocks of code and have all elements within the block all on the same line. I guess thats a matter of preference though. Myself I like both depending on the type of script and what makes it easier and faster to edit / update.

    Quote Originally Posted by giropets View Post
    7 - Your MySQL Queries
    Sometimes when you're writing a PHP script that includes connections to your MySQL database, you may run into a few problems. Most everyone that had MySQL problems ran a command like this one :
    PHP Code:
    <?php
    mysql_query
    ("INSERT INTO tableName ('id','name') VALUES('1','Mike')");
    ?>
    ..and they figure out that it's not inserting into their database. Here's the solution to this :
    PHP Code:
    <?php
    mysql_query
    ("INSERT INTO tableName ('id','name') VALUES('1','Mike')") or exit("MySQL Error :  " mysql_error());
    ?>
    Another way you can do this is:
    PHP Code:
    <?php
    mysql_query
    ("INSERT INTO `tableName` SET `id` = '1', `name` = 'Mike'") or die(mysql_error());
    ?>

    Quote Originally Posted by giropets View Post
    10 - Printing out a Huge Chunk of HTML at a Time
    Well, I'm sure that many of us found a way to get around this, but I'd like to share with you a few of the ways you can do it.

    1 - Break off your PHP-code, print the HTML, and start your PHP-code up again. (I don't prefer doing this as it looks pretty unprofessional to me).
    2 - Adding backslashes to each HTML tag. (It works, but takes forever to do).
    3 - Using the echo/print command, but without having to do much work. (I recommend) :
    PHP Code:
    <?php
    // Showing a huge chunk of HTML at a time //
    echo<<<END
    <font face="Verdana" color="Orange" size="3">Large, Orange Text in Font Size 3</font>
    <br><br>
    More HTML down here..
    <br><br>
    <div align="Center">Centered text</div>
    END;
    ?>
    I have to say this is a great way to do it!

    Thanks for sharing, very good yet brief overview!
      0 Not allowed!

  28. #68
    Join Date
    Nov 2010
    Location
    USA
    Posts
    48
    wow this was very informative, and bookmarked it for future reference. Nice work on the tutorial man.
      0 Not allowed!

  29. #69
    Another great tip: always use PDO instead of mysql API, it is much easier to code and safer.
      0 Not allowed!

  30. #70
    Use HTML Purifier if you want to filter out any XSS, code injection, fixed improper HTML syntax or malicious code. It's so simple to filter out all bad codes with just a line of PHP code.
      0 Not allowed!

  31. #71
    To save time benchmarking,
    http: / / net-beta.net / ubench
      0 Not allowed!

  32. #72
    Great simple tips that can save you hours of debugging because theres less chance of you missing a microscopic flaw because your code is neat.
      0 Not allowed!

  33. #73
    Including files in your scripts. Apparently using single quotes and include is faster:

    include 'my_file.php';
      0 Not allowed!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •