Jump to content
Larry Ullman's Book Forums

Chapter 12 Pursue Number 3


Recommended Posts

Hello guys, this is my first question as well as my first post in this community

 

Add code to the handling of the $errors variable on the login page that uses a foreach loop if $errors is an array, or just prints the value of $errors otherwise.

 

what im thinking is using is_array() function, to test $errors :

 

if( is_array($errors) )

 

then i tried to use var_dump() to test $errors (even if there is only 1 error) unfortunately its still an array.

 

i cant figure it out, i really appreciate any help, but please dont post any code, just tell me what to do :)

Link to comment
Share on other sites

sendy, welcome to the forums.

I hope you find them as helpful as you're finding Larry's book.

 

I think you're on the right track with the is_array function. I think you just need to flesh things out a bit more.

My interpretation of Pursue #3 is that you want to test whether $errors is an array or not. If it's array, you should use a foreach loop to cycle through it, and if it's not an array, then you should just print out the (scalar) value stored in $errors.

 

Does that help?

Link to comment
Share on other sites

i think the problem lies here :

$errors = array();

if(empty($email))
{
$errors[] = "You forgot to enter the email";
}
else
{
$email = mysqli_real_escape_string($conn, trim($email));
}

if(empty($pass))
{
$errors[] = "You forgot to enter the password";
}
else
{
$pass = mysqli_real_escape_string($conn, trim($pass));
}

 

because i use $errors = array();

 

what im thinking is like this, a bit mess up but solve the problem even tough its not the best way

$errors = '';
if(empty($email))
{
$errors = 'You forgot to enter the email';
}
else
{
$email = mysqli_real_escape_string($conn, trim($email));
}
if(empty($pass))
{
if(!empty($errors)) //if there is a string in $errors
{
 $tmp = $errors;
 $errors = array();
 $errors[] = $tmp;
 $errors[] = 'You forgot to enter password';
}
else
{
 $errors = 'You forgot to enter password';
}
}
else
{
$pass = mysqli_real_escape_string($conn, trim($pass));
}

 

then i use

if(is_array($errors))
{
   foreach($errors as $error)
   echo $error;
}
else
{
   echo $errors
}

 

i dont know any better way to do this, if anyone can give me some tips please do, i will really appreciate it :D

Link to comment
Share on other sites

ermm i dont get it about :

"You can assign keys to a not yet existing array in PHP. It will then create it for you."

 

i already tried to search google but didnt found anything , maybe it was because of wrong keywords..

do you have any link or reference about it?

Link to comment
Share on other sites

The trick is not to declare $errors = array(); Using $errors[] = 'message'; will also create the array if it does not exist. That way, $errors will only be an array if an error message is set. Else, it will be null.

 

Generally, I would instead use empty($errors) to check, but That was not the task here. ;)

Link to comment
Share on other sites

sendy, after looking at the book again, I'm a bit confused by what Larry is asking for as well. Pursue #3 seems to be asking you to test whether $errors is an array or a string. If it's an array, you need to cycle through each value, whereas if it's a string, you only need to print out the string.

 

The thing that I don't understand though is why you'd choose to have a string for $errors over an array in the first place.

Truth be told, I didn't go back through the whole chapter again, so maybe there's some important caveat that I'm missing as well, but I am a bit confused by Pursue #3 as well.

 

Hopefully Larry will come in and clarify things a bit.

Link to comment
Share on other sites

The premise of the question is that by writing the code to check if $errors is an array, the error display code would make fewer assumptions and be more flexible. Since there's a separation from where the errors are created and where they are used, the extra code can be useful. And the answer, of course, is just:

 

if (is_array($errors)) {
   // foreach loop
} else {
   echo $errors;
}

Link to comment
Share on other sites

After my less than useful last post, I thought about it overnight. I came up with this. Is this the sort of thing that would satisfy the question?

 

<?php
//define variables
//$wrong1 = 'This the wrong1';
$wrong1 = '';
//$wrong2 = 'This is the wrong2';
$wrong2 = '';
if(empty($wrong1)){//if var is empty assign message to var
$err1 = 'Wrong1 empty.';
}else{//no worries
echo 'Wrong1 is right<br />';
}
if(empty($wrong2)){//if var is empty assign message to var
$err2 = 'Wrong2 is empty';
}else{//no worries
echo 'Wrong2 is right<br />';
}
if(empty($wrong1) && empty($wrong2)){//if both empty
$error = array($err1, $err2);//create an array of error messages
}elseif(empty($wrong1)){//only one wrong
$error = $err1;//assign a message to $error
}elseif(empty($wrong2)){
$error = $err2;
}else{
$error = 'No problems.';//if all variables have values
}

if(is_array($error)){//is it an array?
foreach($error as $msg){
echo " - $msg<br />";//display the error messages
}
}else{
echo " $error";//only one value missing
}
?>

Link to comment
Share on other sites

For the sake of satisfying the purpose of the if/else statement and providing an answer to Pursue #3, I think your code is fine, Graham, but that's why I have an issue with this pursue in the first place. I really don't understand the point of it, as it takes a fair amount of extra effort to force the errors variable between a string and an array.

As such, while I get what Larry is asking for, I don't understand why he's asking it.

Link to comment
Share on other sites

You should read my previous post in which I explain why. Also, there's no "forcing" of the errors variable between an array and a string. The code written in the book uses $errors as an array and then the code that displays the errors ASSUMES $errors is an array. As you should know, assumptions in programming tend to be bad, especially when code in spot Y is making assumptions about what code in spot X is doing. Thus, the prompt makes a suggestion as to how the code could be written within that assumption. As I already explained.

Link to comment
Share on other sites

Larry, I read your previous post several times before posting my last reply, and I still don't get it. (Maybe I'm just being really dense about this one. Sorry.)

 

I can't understand the purpose of the else statement though. Basically, if $errors is not an array, then the code just echoes $errors under the assumption that it's a scalar (likely a string) value, right? However, when would that ever be the case?

 

As Graham demonstrated, forcing the $errors variable into an array in some cases and a non-array in other cases seems to add a lot of unnecessary overhead and headaches.

 

Again, I'm obviously missing the point here, so I'd appreciate a little bit more of an explanation.

I totally trust you know what you're talking about, but I wanted you to know why I can't understand why you're doing what you're doing.

Link to comment
Share on other sites

I'm not trying to be rude, Larry, I'm asking for an honest explanation, that's it.

I wouldn't be asking (again and again) if I understood.

 

It's just that if you're using a variable as an array, why would it maybe be a string or another scalar value instead? I don't understand that.

Certainly, you can force variables to be other types, but why would you ever want to do that in this case?

As the coder, wouldn't you always be aware of whether the variable is a string or an array and just stick with that?

In short, I feel like if you have to add the if/else statement you suggested because you're unsure of what type a variable might be (at least, in this case), then you're got some bigger issues to deal with in your code.

 

If you don't want to explain your reasons or reply to this, that's fine, but the pursue question isn't that clear or else I wouldn't still be wondering after thinking about it quite a bit and sendy would not have asked about it in the first place.

Also, I've read the entire post several times, and it's still not clear.

Link to comment
Share on other sites

Finally, I get it. Thanks for the second post, Larry. That was the one that clinched it for me.

I had the same problem as HartleySan and had to read the posts several times. Still puzzled, I walked away from it and had a Eureka moment about an hour later.

My code above is unnecessary. You don't have to force the error to be string or array, but I wouldn't have got it without all the input from the Advanced Members.

All the best for the New Year to you all.

Link to comment
Share on other sites

I'm going to end my commenting on this topic by saying that I don't think this is a good pursue question.

I totally get what you're aiming for, Larry, but this particular example is simply too arbitrary to serve its purpose well.

 

By the rationale posed in this pursue question, every time before we want to use a variable, we have to first check the variable type using an if statement. This is total overkill for a vast majority of variables. For 99% of variables, we can safely assume what the type is at all times (since we, the coder, will be completely aware of the variable type having coded it ourselves).

 

I'm as much a believer as anyone in using === and !== instead of == and != to check variable type, but executing an extra if statement every time to check a variable is unwarranted.

There are times when a variable type should be checked, I agree, but the $errors variable is not a good example of this, as the variable is obviously always an array.

 

That's why I have issue with this pursue question, Larry.

Link to comment
Share on other sites

 Share

×
×
  • Create New...