SitePoint Sponsor

User Tag List

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 79
  1. #26
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK so how the hell does it process it if it doesnt yet EXIST (see this type stuff makes me NUTS!)
    First you can tell if it is set yet with isset($myvar). Second if it is not set it treats it like null. And what do you mean "if it doesnt yet EXIST"? You created a name and you haven't assigned value to it yet. What would you expect it to be besides null?
    $myString = "hello";
    $myString++

    whats the result of that mess?
    You really don't want to know. And I doubt many PHP programmers know either. But it is internally consistent. Try this to see what it's doing:
    $s = 'yyy';
    $s++;
    echo "$s<br/>";
    $s++;
    echo "$s<br/>";
    Honestly, this stuff is just language trivium. If small changes like the many listed in this thread actually bother you then stick with what you are confortable with.
    Christopher

  2. #27
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'hello'++ = 'hellp', doens't it?

    so your telling me PHP falls back to the "old school" of anything besides a 0 or negative number is true?
    Yeah. That's why I never go "if ($var)". I always go "if ($var === true)" if I mean to do a boolean (or if course, "if ($var !== false)").

  3. #28
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so your telling me PHP falls back to the "old school" of anything besides a 0 or negative number is true?
    i get to confuse u more...actually negative numbers are true, also...anything besides 0 or null is true.

    edit: actually anything besides 0 or null will evaluate to true, i dont think it is true...not that it makes a difference

  4. #29
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by XtrEM3
    actually anything besides 0 or null will evaluate to true,
    Not quite: http://www.php.net/manual/en/languag...oolean.casting

    When converting to boolean, the following values are considered FALSE:

    • the boolean FALSE itself
    • the integer 0 (zero)
    • the float 0.0 (zero)
    • the empty string, and the string "0"
    • an array with zero elements
    • an object with zero member variables (PHP 4 only)
    • the special type NULL (including unset variables)


    Every other value is considered TRUE (including any resource).
    Douglas
    Hello World

  5. #30
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dc dalton
    Code:
             public void setFirstName(String firstName) {
    		firstName = firstName;
    		}
    ...

    whats the result of that mess?
    That's a mess to me

    Give me attr_writer :first_name any day!

    Douglas
    Hello World

  6. #31
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    public void setFirstName(String firstName) {
            
    firstName firstName;
            } 
    A mess?...

    Code:
    public
    This one should be obvious huh?

    Code:
    void
    Return is void, ie Nothing is returned from the method in question.

    Code:
    (String firstName)
    The parameter has to be typeof string. This is the sort of stuff that PHP5 lacks and it really does disturb me, in that not that PHP5 lacks it, but as to why PHP5 lacks it, and why didn't the developers of PHP5 not implement this??

    Ie

    PHP Code:
    // PHP5 does not support this for example
    $command = (DataSource$helper -> getPageData();
    ... 
    PHP isn't Java, but it would just makes things a lot easier to work with PHP, considering that PHP5 is meant to be aimed towards larger developments. Sorry for going off topic btw folks, I've had my rant

  7. #32
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    PHP Code:
    public void setFirstName(String firstName) {
            
    firstName firstName;
            } 
    A mess?...
    I know what it does, I just don't see why you would want to write it for every get/set it your app. Guess that's why you need an IDE for Java.

    Douglas
    Hello World

  8. #33
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OH MAN AM I AN IDIOT, I wrote this long detailed reply and mistakenly closed the browser in preview .... DAMMIT! Ok let me try this again:

    I will agree that this:

    firstName = firstName;

    is quite messy and will confuse a lot of java newbies ... thats why I used the explicit this.firstName when I code it ... especially if I will be having others work on my code... it basically works out that the variable firstName that is being assigned is local to the method ONLY ... then the variable its being assigned to is the global within the object ... I know FUGLY, again thats why I use the this.

    And yes Dr. is right ... the method is locked down so ONLY what is delared as the return type may be returned ...... that I actually like a LOT, but again thats Java.

    I do appreciate the banter, examples and comparisions made here ... they are helping me compare what I know to what Im trying to learn!

    So here's a final question and then I am off for a 11 hour drive to pick my son up from college! OH FREAKING JOY!

    In Java I usually set up a main controller for a site and then call it with a page parameter so I can then serve up the right page, turn links on & off, change title tags and meta tags .. as we know with Java I create an instance of the object that holds the code and just pass the page parameter to it so I can do all these little utilities.. Now in PHP Ive been able to do this (once) by using code like this:

    $upper = new PageUpper();
    $upper->doTop($pageName);

    But the class PageUpper was within the same file... Ill admit its VERY Javaish .... but when I THOUGHT I could do something similar in PHP by using an incude or a require but have yet NOT been able to get these external files to recognise the parameter to do what i want .....

    I can do all these fun type things when I have ALL the code in one file but that just makes my skin crawl! AND Ive had no problems with includes or whatnot when i didnt try to pass any data around between them. So what the heck am I missing here!

    OK, thats all for me today .... gotta eat and then do the drive FROM HELL! Ill see ya tomorrow after I recoup!

  9. #34
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    I know what it does, I just don't see why you would want to write it for every get/set it your app. Guess that's why you need an IDE for Java.

    Douglas
    BTW, I DONT USE or NEED any IDE .. I code EVERYTHING by hand!

  10. #35
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dc dalton
    So what the heck am I missing here!
    a.php:

    PHP Code:
    <?php

    include 'b.php';

    $b = new B;
    $b->talk('Bob');

    ?>
    b.php:

    PHP Code:
    <?php

    class {

      function 
    talk($name) {
        echo 
    "Hi $name!";
      }
      
    }

    ?>
    a.php outputs "Hi!".

    Cheers,
    Douglas
    Hello World

  11. #36
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dc dalton
    BTW, I DONT USE or NEED any IDE .. I code EVERYTHING by hand!
    Broken caps lock key too?

    I was just pointing out things that are flaws from my perspective in PHP and Java.

    Why should you have to write getters and setters by hand, even if they are just basic direct in and out stuff?

    Edit: note, I wasn't going for just exposing public vars, that tends to making the simple things really simple, but a pain to make more complicated (adding logic, only having writers etc). See my post above: I'd like to see "attr_writer :first_name, :last_name" in PHP and Java - though you need dynamic classes to do that.

    Douglas
    Last edited by DougBTX; May 4, 2005 at 04:52.
    Hello World

  12. #37
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by dc dalton
    But you are correct in your statement, because the variable firstName is declared as private you could NOT access by

    System.out.println(jane.firstName);

    Matter of fact it wouldnt even compile, the compiler would tell you it has private access..
    Could you then do jane.firstName if firstName was public? If so, what's the point of get/sets?

  13. #38
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Young Twig
    Off Topic:


    Could you then do jane.firstName if firstName was public? If so, what's the point of get/sets?
    You could, but that would let anything touch firstName, which you don't want.

  14. #39
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you then do jane.firstName if firstName was public? If so, what's the point of get/sets?
    In trivial cases getters/setters are redundant, since you can just expose the property by making it public. However - it's a good practice to use getters/setters, since that allows you to attatch logic to the action later on, should you need it.
    Some languages (such as php) allows you you can use magic getters/setters (I'm sure they have a proper name, but I don't recall it), witch allows you to attatch a function to a public property. This way, you will use the class as if you are accessing the property directly, but in fact a method is called. See overload

  15. #40
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    a.php:
    PHP Code:
    <?php
    include 'b.php';
    $b = new B;
    $b->talk('Bob');
    ?>
    b.php:
    PHP Code:
    <?php
    class {
      function 
    talk($name) {
        echo 
    "Hi $name!";
      }
    }
    ?>
    a.php outputs "Hi!".

    Cheers,
    Douglas

    Thanks thats exactly what I was looking for! Now why didnt they put that in this stupid book!

    And Twig ... the idea of making all the variables private and then use setters / getters is called Encapsulation and is the first step (one of many) into such things as Java Beans which are super powerful!

    The idea is YOU control what cant and cant happen to the variable, via tests etc within the setter method... it allows you to lock down that variables data in so many more powerful ways its not funny.. I have used this exact premise to do server-side data validation .... works realy really nice!

  16. #41
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's good practice to use setters/getters/private variables anyway, I thought. I always use it. It's one less place you can hang yourself from.

  17. #42
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, alright. Thanks.

    Oh, by the way, Dave. If you haven't already, get into the habit of using single-quotes for strings in PHP. Double-quotes are slow and annoying.

  18. #43
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    It's good practice to use setters/getters/private variables anyway, I thought.
    Absolutely! I always lock everything down a tight as possible!

    Quote Originally Posted by Young Twig
    Oh, by the way, Dave. If you haven't already, get into the habit of using single-quotes for strings in PHP. Double-quotes are slow and annoying.
    OH thats really gonna screw me up too cause single ' in Java are for chars strings are delimited by " ... does it really make that much difference?

  19. #44
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dc dalton
    does it really make that much difference?
    Nope.
    Hello World

  20. #45
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dc dalton
    Absolutely! I always lock everything down a tight as possible!



    OH thats really gonna screw me up too cause single ' in Java are for chars strings are delimited by " ... does it really make that much difference?
    I say yes, most say no. The single quotes definitely ARE slower, it's a matter of how much. On top of that, because you use quotes so frequently, it WILL add up. With more and more people visiting the site at the same time, you would even see your loads marginally as you grow. It's probably not the first thing that should be on your mind but I'd definitely advocate single quotes.

    Plus, in my opinion, single quotes look better.
    PHP Code:
     <?php
     $a 
    'Hello '.$world;
     
    $a "Hello $world";
     
    ?>
    You can actually identify the variable (assuming you're using syntax highlighting), plus then with arrays you don't have to go "Hello {$var['world']}" since you'll be going 'Hello '.$var['world'];.

    C is the same as Java. Chars are single, strings are double. It confuses me every time I go back to use C.

  21. #46
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    The single quotes definitely ARE slower...
    You did it again.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  22. #47
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Errrrr.

  23. #48
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    I say yes, most say no.
    Despite my 'nope' I notice now that in my code sample quoted above by Dalton that I used singles when there were no vars in the string, and doubles when there were - perhaps it is built in habit to use the fastest way now It isn't something I would stress about though.

    Douglas
    Hello World

  24. #49
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo "<div class=\"asdf\"><label for=\"price\">Price: \$</label><input type=\"text\" name=\"price\" value=\"{$info['price']}\" />";

    //vs.

    echo '<div class="asdf"><label for="price">Price: $</label><input type="text" name="price" value="'.$info['price'].'" />'

  25. #50
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Despite my 'nope' I notice now that in my code sample quoted above by Dalton that I used singles when there were no vars in the string, and doubles when there were - perhaps it is built in habit to use the fastest way now It isn't something I would stress about though.

    Douglas

    That fastest way (correct me if I'm wrong) is
    PHP Code:
    echo 'Hello' $name// fastest 


Bookmarks

Posting Permissions

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