jimfog Posted January 11, 2013 Share Posted January 11, 2013 I am trying to use trim() on the values of an array nut it does not work because according to the manual it only accepts strings.Here is some code to see what I am talking about: $pricelist['price1']=$_POST['price1'];trim($pricelist['price1']); The user has entered the price in a form and I am passing that a value to an array of my own-but as I said above it does not work. I get error message...'trim expects string,array given'. WHat is wrong? Link to comment Share on other sites More sharing options...
jeffman Posted January 11, 2013 Share Posted January 11, 2013 If $_POST['price1'] is in fact a string, that should work. Try var_dump($_POST); to see exactly what data types it contains. Link to comment Share on other sites More sharing options...
jimfog Posted January 11, 2013 Author Share Posted January 11, 2013 well...here is the strange thing.I tried var_dump both on the value as it is passed on the POST array as it comes from the formand in the array I created and here is the output: var_dump on $_POST: string 'pkpok' (length=5) var_dump on $pricelist array: array (size=1) 'price1' => string 'pkpok' (length=5) So, yes in both cases we have string(pkpok is just a testword) but var_dump in the post array does not output that the type of the variable is an arrayas this happens with $pricelist array. Link to comment Share on other sites More sharing options...
jeffman Posted January 11, 2013 Share Posted January 11, 2013 I cannot duplicate this. Maybe justsomeguy has an answer Link to comment Share on other sites More sharing options...
justsomeguy Posted January 11, 2013 Share Posted January 11, 2013 According to what I see it looks fine, it's obviously not an array. Maybe seeing more code would help. The return value of trim isn't being saved, but that doesn't cause an error. Link to comment Share on other sites More sharing options...
thescientist Posted January 11, 2013 Share Posted January 11, 2013 what happens if you trim $_POST['price1'] Link to comment Share on other sites More sharing options...
jimfog Posted January 14, 2013 Author Share Posted January 14, 2013 what happens if you trim $_POST['price1'] Yes...here is the weird,despite POST being an array too here is what var_dump outputs:string 'pkpok' (length=5) while var_dump on the pricelist array gives me what I mentioned above. But this is not the end of the story-here is some more code: $pricelist['price1']=$_POST['price1'];function profile_check($required,$services,$pricelist){ global $required,$pricelist;echo 'the below is vardump POST price1<br>';var_dump($_POST['price1']);//check herevar_dump($pricelist['price1']);//check here //validate the price if(val_prices($pricelist['price1'])) return true; else false; val_services($services); } function val_prices($pricelist){ global $pricelist; echo 'the below is var_dump pricelist in the val_prices function<br>'; var_dump($pricelist); $price1tr=trim($pricelist);//check here if(is_numeric($price1tr)) return true; else return false; } The problem appears within Val_prices and not the function profile_check.Var_dumping the 2 arrays on profile_check give this-for both: string 'pkpok' (length=5)string 'pkpok' (length=5) while var_dumping the pricelist array within val_prices gives this-which also gives the error mentioned above: array (size=1) 'price1' => string 'pkpok' (length=5) I do not know why there is such behavior from PHP. Link to comment Share on other sites More sharing options...
birbal Posted January 14, 2013 Share Posted January 14, 2013 $pricelist itself is an array. in val_price() you are passong $pricelist. $pricelist['price'] is an element of array $pricelist which is itself a string, thus var dumping it shows you the string in other function. Link to comment Share on other sites More sharing options...
jimfog Posted January 14, 2013 Author Share Posted January 14, 2013 $pricelist itself is an array. in val_price() you are passong $pricelist. $pricelist['price'] is an element of array $pricelist which is itself a string, thus var dumping it shows you the string in other function.I do not understand...how this is related to our problem. Link to comment Share on other sites More sharing options...
birbal Posted January 14, 2013 Share Posted January 14, 2013 in val_price() you have declared $pricelist as global. where as you have declared $pricelist as an array in the first line $pricelist['price1']=$_POST['price1']; it is actually creating an array $pricelist and assigning the $_POST['price1'] to $pricelist element "price1".In val price when you use global it is overwriting the local variable of $pricelist (which you have used as parameter). so at the time you are using trim() you are using $pricelist array.You should use var_dump() just before the problemetic line so that you can examine the variable well. Link to comment Share on other sites More sharing options...
thescientist Posted January 14, 2013 Share Posted January 14, 2013 stop using globals (it kind of defeats the point of functions) and start showing more code from the beginning. please. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 14, 2013 Share Posted January 14, 2013 This is the problem: $price1tr=trim($pricelist); You already know that $pricelist is an array, you know that trim takes strings and not arrays, so that's a problem when you send an array to trim. Link to comment Share on other sites More sharing options...
jimfog Posted January 15, 2013 Author Share Posted January 15, 2013 This is the problem: $price1tr=trim($pricelist); You already know that $pricelist is an array, you know that trim takes strings and not arrays, so that's a problem when you send an array to trim. Υes but POST is also an array but there is not problem using trim with it.I think birbal's explanation must be the correct one-not that yours is wrong. Link to comment Share on other sites More sharing options...
jimfog Posted January 15, 2013 Author Share Posted January 15, 2013 (edited) in val_price() you have declared $pricelist as global. where as you have declared $pricelist as an array in the first line$pricelist['price1']=$_POST['price1']; it is actually creating an array $pricelist and assigning the $_POST['price1'] to $pricelist element "price1". In val price when you use global it is overwriting the local variable of $pricelist (which you have used as parameter). so at the time you are using trim() you are using $pricelist array.You should use var_dump() just before the problemetic line so that you can examine the variable well. Despite I have understood in general what are you talking about I have a question to make.Suppose I do not declare in val_price pricelist as global and and the parameter passes with no problem.Yes...but the parameter variable-is in it a array either way?The code works OK now.I am just having difficulty to understand why when $pricelist is declared globalthe problem appears and when not the problem disappears. Ok, you said...because it overrides the parameter. But the parameter is $pricelist...Sorry...for the long message. Edited January 15, 2013 by jimfog Link to comment Share on other sites More sharing options...
justsomeguy Posted January 15, 2013 Share Posted January 15, 2013 Υes but POST is also an array but there is not problem using trim with it.Uh, yeah there is a problem using $_POST with trim. $_POST is an array. Trim does not work on arrays. That's obviously a problem. You're sending $pricelist to trim. $pricelist is an array. Trim does not work on arrays. That is a problem. This isn't a complicated issue. Go ahead and try it if you don't believe me, run trim($_POST) and see what happens. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now