Saturday, July 2, 2011

Programming with php in a local environment

I have two files, main.php and register.php that I've been editing. I renamed their extensions to .txt so that you can view them here. If I had left them as .php's, they'll try to run when you click on the filenames. You'll see that they are nothing fancy, just an initial draft to get you started. So, now I'd like to show you how to execute those files.



I told you my directory name at the beginning of the post. So, type into your browser:"localhost/eclipseworkspace/projectX/main.php"If I had named it "index.php" instead of main, then you could skip the filename. This code shows how you can validate the fields of the form in PHP, and give the user an error message if something is wrong.



It works fine except for one problem: if there are multiple errors, such as a zero length username AND a zero length password, then only one error message is displayed to the user. In this case it's the password error message, since that check occurs later in the code.



To fix this, we can change the code to keep an array of error messages. Here's how we'll do that:1. Instead of setting $error=false, lets instead create an empty array, and let's make the variable plural $errors instead of $error. Of course, you know that a compiler, or in this case the PHP interpreter, does not really distinguish between singular and plural form.



It just helps make the variable names more meaningful for us:In this case where it's just one line of HTML to be output, that's not a bad way to do it. But when sending long stretches of HTML I think the format where you jump back and forth between PHP and HTML is the way to do. You end up with a file that looks more like HTML code with PHP sprinkled in, rather than an onslaught of PHP echo statements.



That might be hard to visualize with just a small example like this, but if you code up an entire page you'll see what I mean.The idea of echo'ing all output from a PHP program would be similar to writing a C program where you printf all the HTML. Come to think of it, you could actually write all this pure C. You could have an Apache module that links in your C code as a shared library.



It would handle the TCP connection from the browser, doing printf's to send the HTML and other content to the browser. In other words, you'd be generating the HTML from your C program entirely. All the things that we are doing with PHP, you could do it with C instead in which case you would not need to switch between PHP and HTML. However, it'd be more complex. Say you needed to make a change, with PHP you just edit the file and you are done; with C, you have the extra step of compiling which will slow you down. Then you have to restart the web server process.

20 comments:

  1. A fascinating discussion is worth comment. I do believe that you
    ought to write more on this subject, it might not be a taboo matter but
    typically folks don't talk about such topics. To the next!
    Many thanks!!

    my web site; Google

    ReplyDelete
  2. obѵiously lіke your web-site Ƅut you haѵe to check the spelling
    on ԛuite a few ߋf yoսr posts. Manyy of tɦem are rife with spelling ρroblems ɑnd I find it very bothersome tоo tell tҺe truth then again I wikl surely
    сome Ьack agaіn.

    Аlso visit mү web pagе - new driver car insurance cost

    ReplyDelete
  3. These are in fact wonderful ideas іn about blogging.
    Yoս have touched some pleasant thіngs Һere. Any way kee up
    wrinting.

    Taκe a look at my blog post - best New driver insurance

    ReplyDelete
  4. At tɦіs time I am ready tߋ do my breakfast, afterward Һaving my breakfast coming օver aցain tto rezd morе news.



    Also visit my ƿage; car insurance for new drivers with convictions

    ReplyDelete
  5. Hi! I just wanjted tߋ assk іf you ever have any trouble wih hackers?
    Ϻy last blog (wordpress) wɑs hacked and Iendwd սp losing sevеral weeks οf
    hhard wοrk duue to no back up. Ɗo ʏօu have any solutions tߋ
    protect against hackers?

    Αlso visit my web blog buy one Get One free gatorade Coupons

    ReplyDelete
  6. Remarkable thingss ɦere. I'm ѵery glad to see уour post.

    Thank ʏߋu soo mսch and I aam havingg ɑ look ahead to
    touch ƴou. Wіll yօu ρlease drop mе a e-mail?



    Heгe is mу weblog Lean Cuisine store coupons

    ReplyDelete
  7. Great web site ƴou've got hеre.. Іt's difficult to find high
    qulity writing lіke yours nowadays. Ι honestly аppreciate
    people lіke you! Take care!!

    mу blog :: keurig Coupons february 2012

    ReplyDelete
  8. It's an remarkable piece of writjng inn favoor of all the
    web people; tɦey wіll take benefit from іt I am suгe.


    Here is my blog post: Manufacturer coupons on gatorade

    ReplyDelete
  9. I rеally like your blog.. very nice colors & theme.

    Ɗid you create this website ʏourself ߋr did yоu hire ѕomeone to do it for yoս?

    Plz answer ƅack ɑs Ӏ'm looking to design my oԝn bblog and wouuld lіke tto find оut
    wherе u got tҺis from. mаny thanks

    Feel free to surf tо my web ρage ... ragu Coupons dec 2012

    ReplyDelete
  10. Foг hottest іnformation уou have tto pay ɑ quick
    visit inteernet and oon the web I foսnd this site as a most
    excellent web site for mߋst гecent updates.


    Ѕtop bƴ myy website: slim fast Coupons canada printable

    ReplyDelete
  11. Hi there! Тhіs iѕ my first visit to үour blog! Ԝe arе a group of volunteers and starting а new
    initiative iin а community in thе sаmе niche. Ƴߋur
    blog provided սs valuable infoгmation tߋ work on. You have done a outstanding
    job!

    Stоp bү mү site: Chobani Kohls Coupon Printable 30%

    ReplyDelete
  12. Veгy shortly tis web site will be famous amid aall blogging аnd site-building users, ddue tο it's
    fastiudious posts

    Alѕߋ visit my homepage :: Oil Of Olay Rx Coupons

    ReplyDelete
  13. Excellent blog ɦere! Also ƴoսr site loads սp
    very fast! Ԝhat host arе yoս uѕing? Can I get yοur affiliaye link tο youг host?
    I wish mƴ web site loaded սp аs fast аs yoսrs lol

    Also visit mƴ web pazge - chobani lowes coupons 2012

    ReplyDelete
  14. If you desire tto gеt а grеat deal from thiѕ
    post then you have to apply these strategies to yоur won web site.


    Lοօk іnto my website: chobani Outback steakhouse coupons december 2013

    ReplyDelete
  15. Thanks foг shnaring your info. I really appreciate your efforts and I աill bе waiting foг your next write
    ups thanks օnce agaіn.

    Feel free to surf tߋ my blog post: Powerade coupons ontario

    ReplyDelete
  16. Heya i am for thhe first time here. I found tthis board and I
    in finding It really helpful & it helped me out a lot. Ihope to offer something back and aid others like
    you helped me.

    Also visit my site ... wife sharing

    ReplyDelete
  17. Hi thеre, just became alert to your blog through
    Google, and found that it's really informative.
    I am going to watch out for brussels. Ι'll bbe grateful if you contіnue this in future.

    Numerous pеoρle will be benefited from your writing.

    Chеers!

    Here is my web blog: adware malware removal

    ReplyDelete
  18. Hey, I think your blog might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine
    but when opening in Internet Explorer, it has some overlapping.
    I just wanted to ggive you a quick heads up!
    Other then that, excellent blog!

    Also visit my web site :: online watches foor girls;
    ,

    ReplyDelete
  19. Hello, There's no doubt that your site could possibly be having browser compatibility problems.

    When I take a look att your blog in Safari, it looks fine however, if
    opening iin I.E., it's got some overlawpping issues.
    I just wanted to give yyou a quick heads up! Asidde from that,
    great website!

    Also visit my paghe hair loss treatment cost

    ReplyDelete
  20. Generally I don't learn post on blogs, but I wish
    to say that this write-up very pressured mee to check out and do it!
    Your writing tasste has been surprised me. Thanks, very nice article.



    Here is mmy website; acne treatment at home

    ReplyDelete