views:

63

answers:

3

hmm i got a homework, its 2 hours and i still have no clue on it :|

like this

$sessions['lefthand'] = 'apple';
$sessions['righthand'] = '';
$sessions['head'] = 'hat';
$sessions['cloth'] = '';
$sessions['pants'] = '';

// here is the homework function
CheckSession('lefthand,righthand,head,cloth,pants');

we have some string "lefthand,righthand,head,cloth,pants" question is : " how can we check if the five session is not null or exist and display which session is empty ( if there is an empty session ) if all exist then returns a true ?

empty righthand , pants, and cloth.

this is how i think about it

  1. explode it to arrays
  2. check one bye one if !null id there is a

here is the progress that ive made *edit4 , :)

function CheckSession($sessions){
$all_sessions_exist = true;
$keys = explode(',',$sessions);
$error = array();
    // Search for Session that are not exist
    foreach ($keys as $key) {
        if (!isset($_SESSION[$key]) && empty($_SESSION[$key])) {
            echo "no $key</br>";
            $all_sessions_exist = false; 
        }
    }
return $all_sessions_exist;
}

Thanks for taking a look

Adam Ramadhan

+2  A: 

Seeing as it's homework, you won't get the solution. You're on the right track though. explode() it by the delimiter. You can the loop through it using foreach and use empty() to check if they're set. You can access the sessions like $_SESSION[$key]. Keep an array of the ones that match.

Daniel Egeberg
A: 
function CheckSession($string){
    $all_sessions_exist = true; #this will change if one of the session keys does not exist
    $keys = explode(',', $string); #get an array of session keys
    foreach($keys as $key){
        if(isset($_SESSION[$key])) {
            if(!empty($_SESSION[$key])) 
                echo '$_SESSION['.$key.'] is set and contains "'.$_SESSION[$key].'".'; #existing non-empty session key
            else echo '$_SESSION['.$key.'] is set and is empty.' ;
        }else {
             echo '$_SESSION['.$key.'] is not set.'; #key does not exist
             $all_sessions_exist = false; #this will determine if all session exist
        }

        echo '<br />'; #formatting the output
    }

    return $all_sessions_exist; 
}
cypher
Damn, sorry for posting the whole solution, I somehow didn't realize that this is a homework.
cypher
isset ? empty ? why just not use empty ? is there a difference ? and yes and thanks mate, it is a best-practices spoiler type :)
Adam Ramadhan
If you won't perform an isset check, there will be no difference between $_SESSION['pizza'] = ''; (just empty value) and key pizza not being set at all.
cypher
This may clear things up a little, calling empty($var) is something like if(!isset($var) || strlen($var) == 0);
cypher
Your code is extremely messy.
RobertPitt
@RobertPitt Please explain.
cypher
do i really have to, why are you echo'ing in a function for starters :( then you have the lack of typing standards. instead of the `else {` within the foreach, just use a continue command.
RobertPitt
1. because of the purpose - "question is : " how can we check if the five session is not null or exist and display which session is empty ( if there is an empty session ) if all exist then returns a true ?" 2. the code with continue command would not be self-explainatory and not suitable for a beginner. Also, I do not see how is using continue any better than using if in this context, since ifs are for conditions, by this logic you may just use goto instead of all the flow-control statements :-)
cypher
You could just `return` if one was false, instead of continuing. That way, you don't even need the `$all_sessions_exist` variable, and your script will almost always finish faster.
Jeriko
Well for starters you should of used concatenation for return strings, i belive hash comments dont really belong in php, I never see anyone use them, in regards to continue, it would be faster then an else, POC http://www.smashingmagazine.com/2008/11/18/10-advanced-php-tips-to-improve-your-progamming/ if your a good programmer who understands system architecture you should of advised him not to use echo within functions as functions are part of the logical side of programming and not the View side.
RobertPitt
How will it print out if the remaining variables are set and not empty?
cypher
as i said, you need to understand system architecture to be able to understand that what you told the op is how to be a poor programmer, why not read a C++ / C# book and learn true OOP and system architecture .
RobertPitt
There isn't anything wrong with using hash comments at all, for the rest, you should consider that this is not an example of properly separated MVC architecture designed to handle thousands of request per hour, this is a beginner's homework.
cypher
What does this have to do with OOP?
cypher
Nothing at all, but if you know OOP you would understand the fundamentals of `Functions` / `Methods` / `Object` and waht there uses are for, i dont know why im still here to be honest, your boring me :(
RobertPitt
I know OOP very well, but this question still has virtually nothing to do with it. Or are you suggesting that he should create multiple classes separating application and presentation logic in an object-oriented, NOT object programming language JUST TO do such simple thing while learning the basics of the language? That would be one of the worlds greatest examples of overengineering.
cypher
I never said use OOP Concept for a simple task, im saying that the structure of coding in OOP should also be taken into consideration across the board, you almost admising him to do `WriteMyName('Robert');` directly to the output buffer without any consideration. if he is studying programming then do the right thing and dont lead him down the shitty path. Look were way of the mark here, stfu and get back to work.
RobertPitt
WHY should be "the structure of coding in OOP" taken in consideration "across the board"? Who ever suggested that? When? Why? What exacly is that structure? Don't you confuse OOP with MVC and simillar architectures? Please don't make stuff up. (same as the E_NOTICE comment)
cypher
A: 

Just cleaning up your function

function CheckSession($sessions)
{
    $session = explode(',',$sessions);
    $return = array();
    foreach ($session as $s)
    {
        $return[$s] = (isset($_SESSION[$s]) && !empty($_SESSION[$s]) ? true : false)
    }
    return $return;
}

$sessions['lefthand'] = 'apple';
$sessions['righthand'] = '';
$sessions['head'] = 'hat';
$sessions['cloth'] = '';
$sessions['pants'] = '';

And the checking part

// here is the homework function
$Results = CheckSession('lefthand,righthand,head,cloth,pants');

Edit

//$value1= false; // Commented out so does not get set
$value2= false; //Still set to a bool

error_reporting(E_ALL);

empty($value1); // returns false but still strikes E_NOTICE ERROR
empty($value2); // returns false with no error

Note, empty does not trigger an error but this example would take affect an a lot of other php functions.

RobertPitt
Adam Ramadhan
`isset()` determines weather it actually been set, where as empty just checks if the value is empty, if the item has not been set and you just run empty on an variable thats not set you will get a php NOTICE error
RobertPitt
Check my edit, you should always check to see if a variable is set, because if its not set you dont have to run an extra load of code to make sure its how you want it. in my code if its not set, it skips empty and sets the array value to false.
RobertPitt
@RobertPitt Actually, it won't trigger any error at all, empty() performs an isset check, see http://php.net/manual/en/function.empty.php, quoting "empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."
cypher
Yes I know that, I was just teaching him to always check a variable as not many built in functions check the set state of a entity, why was this rated down, My answer is not about variable states but about his function.
RobertPitt
well i dont know, somebody is, XD dispiteing on which one is better atleast i have excellent answers. thanks guys.
Adam Ramadhan
I can see that, but "empty($value1); // returns false but still strikes E_NOTICE ERROR" is simply not true.
cypher
yea i know its not true, your not getting my point, the point of my comment about isset is to always check variables regardless if the next logincal check bypasses the isset check. its good to know and if he wants to become a programmer he should learn these things.
RobertPitt
By doing it this way, you'll find "$variable;" somewhere in his code very soon :-)
cypher
yea you lost me :/
RobertPitt