wadej30084 Posted February 11, 2020 Share Posted February 11, 2020 Hi there, i have an code <?php $a = [ 0 => 10, ]; $i = 0; $a[$i++] = $i; and question "Explain what the problem is and what you could do to fix it" I read this and I totally not understand what it for and how it use and what problem hidden here. Please help! Quote Link to comment Share on other sites More sharing options...
mconte Posted February 11, 2020 Share Posted February 11, 2020 (edited) ok, so $a[0] will equal 1 what do you want it to do? Edited February 11, 2020 by mconte Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 11, 2020 Author Share Posted February 11, 2020 Just now, mconte said: ok, so $a[0] will equal 1 It's all about using ++$i instead $i++ or what, because I ran this code and stuck on understand what wants from me? Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 11, 2020 Author Share Posted February 11, 2020 I have an question for it "Explain what the problem is and what you could do to fix it" and nothing more, that's why I'm stacked. Quote Link to comment Share on other sites More sharing options...
mconte Posted February 11, 2020 Share Posted February 11, 2020 sure, if you did $a[++$i], it would increment $i first and set a new index $a[1] = 1 Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 11, 2020 Author Share Posted February 11, 2020 17 minutes ago, mconte said: sure, if you did $a[++$i], it would increment $i first and set a new index $a[1] = 1 thanks for advice! Quote Link to comment Share on other sites More sharing options...
requinix Posted February 11, 2020 Share Posted February 11, 2020 20 minutes ago, mconte said: sure, if you did $a[++$i], it would increment $i first and set a new index $a[1] = 1 Not necessarily. PHP makes no promise that the $i on the right happens before or after the ++$i on the left. Quote Link to comment Share on other sites More sharing options...
mconte Posted February 11, 2020 Share Posted February 11, 2020 19 minutes ago, requinix said: Not necessarily. PHP makes no promise that the $i on the right happens before or after the ++$i on the left. https://www.php.net/manual/en/language.operators.increment.php Quote Link to comment Share on other sites More sharing options...
requinix Posted February 11, 2020 Share Posted February 11, 2020 9 minutes ago, mconte said: https://www.php.net/manual/en/language.operators.increment.php Yes, that describes how one pre-increment operation works on a variable. That page does not explain how one or more pre- or post-increment operators on the same variable work when in the same expression. The concept is called a sequence point. Look into it: most everything you'll find will talk about C/C++, but it applies to PHP as well. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 12, 2020 Share Posted February 12, 2020 I personally can not see how I would ever write a block of code that looks like this!! What is that supposed to accomplish? Never mind - I don't want to know. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 12, 2020 Share Posted February 12, 2020 33 minutes ago, ginerjm said: I personally can not see how I would ever write a block of code that looks like this!! What is that supposed to accomplish? Never mind - I don't want to know. Obviously it isn't real code someone would write. Not all of it. But that last line, that's something one could reasonably write in some circumstance. Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 12, 2020 Author Share Posted February 12, 2020 Ok, I see you like it, I have another one. Same situation have code piece: function getAdminName() { global $admin; return ($admin['gender'] ? 'Mr' : 'Mrs').' '.$admin['fullname']; } and simple question: "This code isn't ideal. Explain what the problem is and what you could do to fix it" it's all, please help! Today I have no idea what's wrong except this is creepy realization and I do this in totally another way. P.S. ginerjm asking what is it, this is a part of practical tasks of technical php test and I trying do my best to solve it, but even if I fail on exam I need to understand what's it about. All this about: "You need to think globally, we need to see how you think." And like Ross Geller's little hand monkey, but in my head, beating in cymbals plates, with deep understanding of this code problem eyes look. Thanks for help! Quote Link to comment Share on other sites More sharing options...
maxxd Posted February 12, 2020 Share Posted February 12, 2020 The portion of your last code bit in parenthesis is called a ternary operator. I recommend reading up on it as it's something you'll see often if my experience is at all typical. Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 13, 2020 Author Share Posted February 13, 2020 31 minutes ago, maxxd said: The portion of your last code bit in parenthesis is called a ternary operator. I recommend reading up on it as it's something you'll see often if my experience is at all typical. Thanks, for reply. I know what is ternary operator, also I know about latest deprecated features about this operator in 7.4 version, but i don't see here any problem with it, any complex use like 1 ? 2 : 3 ? 4 : 5; , the problem is what i don't understand what I'm must looking for in this piece of code. Any advice? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 13, 2020 Share Posted February 13, 2020 1 hour ago, wadej30084 said: but i don't see here any problem with it, any complex use like 1 ? 2 : 3 ? 4 : 5; But here's the question: what is that expression going to evaluate to? 2 or 4? 1 hour ago, wadej30084 said: the problem is what i don't understand what I'm must looking for in this piece of code. Any advice? The problem is the last line. It uses $i++ and $i in one statement, and that's a no-no. Did you see when I mentioned sequence points earlier? Look into it. Quote Link to comment Share on other sites More sharing options...
maxxd Posted February 13, 2020 Share Posted February 13, 2020 2 hours ago, wadej30084 said: 1 ? 2 : 3 ? 4 : 5; In the name of all that you find holy, please don't do that. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 13, 2020 Share Posted February 13, 2020 1 hour ago, maxxd said: In the name of all that you find holy, please don't do that. Fortunately PHP 7.4 won't let it happen. Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 13, 2020 Author Share Posted February 13, 2020 Hi, maybe I'm saying something wrong, but I'm talking about this piece of code, another from first post: function getAdminName() { global $admin; return ($admin['gender'] ? 'Mr' : 'Mrs').' '.$admin['fullname']; } Help is needed with it, be answering on the question: "This code isn't ideal. Explain what the problem is and what you could do to fix it" Quote Link to comment Share on other sites More sharing options...
Phi11W Posted February 13, 2020 Share Posted February 13, 2020 59 minutes ago, wadej30084 said: I'm talking about this piece of code the convention around here is "New question, new thread". That allows for short, direct answer to short, direct questions instead of long, rambling threads where all the "Goodness" gets lost. 1 hour ago, wadej30084 said: function getAdminName() { global $admin; return ( $admin['gender'] ? 'Mr' : 'Mrs') . ' ' . $admin['fullname'] ; } Some comments on the above: the use of "global" breaks encapsulation, requiring the environment "outside" the function to provide the variable. It is better to pass the data as an argument to the function. What value does admin['gender'] have? Any value passed that resolves to true will cause the ternary operator to return "Mr" and everything else will return "Mrs". The code makes no attempt to ensure that the array indexes used actually exist; this may or may not be an issue. What if the individual is female and not married? They might object to being called "Mrs". What if the individual is not gender-identifying? They would object most strongly to be referred to by either of the terms used here. Marital status and/or gender are both Personal Data and should be stored in the User's "record" (whatever form that takes) so that it can be managed by/on behalf of the User and changed over time. Regards, Phill W. 2 Quote Link to comment Share on other sites More sharing options...
wadej30084 Posted February 13, 2020 Author Share Posted February 13, 2020 4 minutes ago, Phi11W said: the convention around here is "New question, new thread". That allows for short, direct answer to short, direct questions instead of long, rambling threads where all the "Goodness" gets lost. Some comments on the above: the use of "global" breaks encapsulation, requiring the environment "outside" the function to provide the variable. It is better to pass the data as an argument to the function. What value does admin['gender'] have? Any value passed that resolves to true will cause the ternary operator to return "Mr" and everything else will return "Mrs". The code makes no attempt to ensure that the array indexes used actually exist; this may or may not be an issue. What if the individual is female and not married? They might object to being called "Mrs". What if the individual is not gender-identifying? They would object most strongly to be referred to by either of the terms used here. Marital status and/or gender are both Personal Data and should be stored in the User's "record" (whatever form that takes) so that it can be managed by/on behalf of the User and changed over time. Regards, Phill W. Wow, this a big explanation, many thanks Phill! P.S. I keep in mind about new thread. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 13, 2020 Share Posted February 13, 2020 … or their correct salutation is Dr, or some other title (eg Prof.) that is not gender-related Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 13, 2020 Share Posted February 13, 2020 Phi11w provided an excellent and thorough analysis. In particular consider this data for $admin: <?php $admin = ['fullname' => 'Sally Smith', 'gender' => 'F']; function getAdminName() { global $admin; return ( $admin['gender'] ? 'Mr' : 'Mrs') . ' ' . $admin['fullname'] ; } echo getAdminName(); Do you get what you would expect for a gender 'F' person named 'Sally Smith'? While all of Phi11w's points are valid, I think this is the main thrust of the issue. Even without knowing the scheme to be used, it is unlikely that gender would be a boolean value. Another issue being glossed over, is that 'Mrs' has the connotation of marriage, as well as Barand's point about 'Dr' and other sultations, but that makes things more complicated, and I don't think that was factored into the question, because the existing code has an obvious data typing issue. So an improved function (assuming php7): <?php $admin1 = ['fullname' => 'Sally Smith', 'gender' => 'F']; $admin2 = ['fullname' => 'Randy Jones', 'gender' => 'm']; $admin3 = ['fullname' => 'Pat Samuels']; function getAdminName($admin) { $admin['fullname'] = trim($admin['fullname']) ?? 'unknown'; $admin['gender'] = $admin['gender'] ?? ''; $pre = ''; switch (strtolower($admin['gender'])) { case 'f': $pre = 'Mrs '; break; case 'm': $pre = 'Mr '; break; default: $pre = ''; } return $pre . $admin['fullname']; } echo getAdminName($admin1) . PHP_EOL; echo getAdminName($admin2) . PHP_EOL; echo getAdminName($admin3) . PHP_EOL; Quote Output: Mrs Sally Smith Mr Randy Jones Pat Samuels Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.