SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Location
    Ljubljana
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php - rational coding // elegant code

    Hello,

    I want to give a little make up to my php code and I think its really annoying to check each time if the variable exists or not. Is there any cool way to make this code cleaner:

    Code:
    if (isset($array['key']) && $array['key']) {
    }
    What do you think?

    + another one

    When you write foreach loop for object and you want to count the number of the current loop, how can you write this piece of ugly code to be prettier:

    Code:
    $count = 0;
    foreach($object as $o) {
    //preform sth
    $count++;
    }
    note that as $i => $o won't work if you have an object instead of an array.

    Maybe any other cool suggestions for makeups?

    Thanks for help.
    Get RankTrackr 2.0: Rank Tracker | PopStrap

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You either have to explicitly set a variable somewhere and test for a absence of a default value, or test that your variable is set otherwise if it is not set you will cause an error notice.

    The old rule used to be "always declare your variables at the top of your file".

    PHP Code:
    $var null;

    //some stuff

    if ( $var ) {
    //do your thang

    else

    PHP Code:
    if( isset( $var )){
    // do your thang

    Up to you to choose which way suits you, but

    PHP Code:
    if (isset($array['key']) && $array['key']) {

    seems to contain a double check of the same thing essentially.

    ref : your redundant count variable... try
    PHP Code:
    $pets = array('cats','dogs','mice');

    echo 
    count$pets );
    // gives 3 
    Good questions ... easy answers ...

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Location
    Ljubljana
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    You either have to explicitly set a variable somewhere and test for a absence of a default value, or test that your variable is set otherwise if it is not set you will cause an error notice.

    The old rule used to be "always declare your variables at the top of your file".

    PHP Code:
    $var null;

    //some stuff

    if ( $var ) {
    //do your thang

    else

    PHP Code:
    if( isset( $var )){
    // do your thang

    Up to you to choose which way suits you, but

    PHP Code:
    if (isset($array['key']) && $array['key']) {

    seems to contain a double check of the same thing essentially.

    ref : your redundant count variable... try
    PHP Code:
    $pets = array('cats','dogs','mice');

    echo 
    count$pets );
    // gives 3 
    Good questions ... easy answers ...

    Hi,

    Actually if (isset($var)) is not the same as if ($var), the first will return true if the var exists, the second wil return true if the $var is not false or null.. so they're quite different

    consider this

    if (isset($var['test']) && !$var['test']) {
    }

    will give true if $var['test'] = false;
    will give false if $var['test'] = true; or not defined

    so basicaly I want to skip checking if var isset since it usually doesn't make sense..

    The second example when you used count, I wanted to know where in the loop you're at (at which step), not the full number of elements in the object, beside that if you have an object - count function won't work because its for arrays.
    Get RankTrackr 2.0: Rank Tracker | PopStrap

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I did not read that properly, sorry. You can access the properties of a class like so:

    PHP Code:
    class {
    public 
    $var1 1;
    public 
    $var2 2;
    function 
    meth1(){}
    function 
    meth2(){}
    }

    $b = new ;
    foreach( 
    $b as $c){
    var_dump$c );
    }
    // int 1
    // int 2

    echo count(get_object_vars($b));
    // 2 
    but this will not work if you have private properties, though there is another, messier, way round it.

    Whether that is what you meant by invoking a foreach loop on a single object, I am not sure.

    Good questions incorrect answers

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    To be honest, I find my variable checking when dealing with user input consists of some basic type-juggling or filtering - so do not concern myself with double set/exists tests such as you had in your first point.
    PHP Code:
    // id MUST be an int otherwise the user is tampering data
    if( (int)$_POST['id'] === ){
    // abort
    }

    // OR

    if( (int)$_POST['id']) > ){
    // get on with stuff

    Now if $_POST['id'] is optional, as in the case you have a single form which doubles as being the "Add new thing" or "Edit existing thing" depending on the existence of the id, then yes "is it set?" and "is it > 0?" is a double check I would do, and no I don't know of any other way of making the check smaller/neater.


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
  •