Darkelve Posted September 23, 2010 Share Posted September 23, 2010 Hey, I would like to get some practice in the following areas: - arrays (including multidimensional and associative) - functions and their return values I would appreciate it if anyone could give me/point me to some exercises/challenges. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/ Share on other sites More sharing options...
rwwd Posted September 23, 2010 Share Posted September 23, 2010 Lol! Write a user management class utilising multidimensional arrays for adding users/monitoring users WITHOUT the use of ANY mysql, and write another class to manage the cookies and sessions used for the first class!! That was my remit a few months back, but fortunately I talked them into using mysql (Huge sigh of relief) Seriously though, some sort of login/user management script will do the trick, and set the error_reporting to E_ALL|E_STRICT. Rw Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114416 Share on other sites More sharing options...
Darkelve Posted September 23, 2010 Author Share Posted September 23, 2010 Sorry, no knowledge yet of classes, objects and sessions! Lol! Write a user management class utilising multidimensional arrays for adding users/monitoring users WITHOUT the use of ANY mysql, and write another class to manage the cookies and sessions used for the first class!! That was my remit a few months back, but fortunately I talked them into using mysql (Huge sigh of relief) Seriously though, some sort of login/user management script will do the trick, and set the error_reporting to E_ALL|E_STRICT. Rw Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114440 Share on other sites More sharing options...
rwwd Posted September 23, 2010 Share Posted September 23, 2010 In that case start with something simple like learning how you can use functions to create forms, passing parameters to the functions so that you can alter aspects of the output depending on what parameters are passed. Array's - just learn how to use them, and the functions that manipulate them, learn foreach loops, which are great for looping through arrays.. Build from that, get a hot coffee on the go, and get a decent book too. Have fun. Rw Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114475 Share on other sites More sharing options...
ignace Posted September 23, 2010 Share Posted September 23, 2010 Riddle me this, riddle me that, can you solve this off the top of your hat? echo 'h' . 1 + 2; // Output: ? echo true ? 'hello' : 'world' ? 'foo' : 'bar'; // Output: ? echo 'a' < 'A'; // Output: ? echo false == array(); // Output: ? Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114666 Share on other sites More sharing options...
fortnox007 Posted September 23, 2010 Share Posted September 23, 2010 in case you just want a tutorial on those topics, you could go to w3schools.com/php Also to help yourself you try to read all the possible array functions at php.net and try to apply them in small script just to see what they do. Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114671 Share on other sites More sharing options...
Darkelve Posted September 24, 2010 Author Share Posted September 24, 2010 1. The first line displays: h3 2. The second line was hard. I had to Google for documentation, apparently it is a 'ternary operator'. It displays: foo - first it checks if 'true' is true, which it is, so the value 'Hello' is assigned. Then it goes to the next part of the formula. Since 'Hello' has a value of 'true' (it is true as long as it does not hold the value of 0 or the keyword 'false'), the value 'foo' is assigned and finally displayed. The following formula will display 'bar': echo false ? 'Hello' : 0 ? 'foo' : 'bar'; // Output: ? 3. Third one I had to 'cheat', i.e. execute it to see what is does. It checks if 'a' is smaller than 'A' and returns true or false. When true, it returns 1 and '1' is diplayed, otherwise it returns false, in which case nothing is displayed. I guess it's equivalent to 'echo false'. So I suppose lowercase 'a' comes before uppercase 'A'. 4. The last one dazzled my mind, had to 'cheat' as well. I guess an empty/non-existing array defaults to false? Riddle me this, riddle me that, can you solve this off the top of your hat? echo 'h' . 1 + 2; // Output: ? echo true ? 'hello' : 'world' ? 'foo' : 'bar'; // Output: ? echo 'a' < 'A'; // Output: ? echo false == array(); // Output: ? Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114961 Share on other sites More sharing options...
rwwd Posted September 24, 2010 Share Posted September 24, 2010 ^^^ Excellent, I had to look at the last one twice too - then the penny dropped. Ternary operator's are your new best friend They make things so easy to do, and can reduce the amount of code you write. You can convert if/else clauses into ternary's to make the code a little more elegant. Have Fun. Rw Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1114997 Share on other sites More sharing options...
j.smith1981 Posted September 24, 2010 Share Posted September 24, 2010 Its actually dead easy passing arguements to functions! I thought a few weeks ago it was the hardest thing ever dont have a clue why though grrr. Most of this is based on classes though (the only way in my head I can get them to work, anyone that wants to convert these into a more procedural approach, I dont mind at all! Here's a very simple one I have been working on (taken allot from a book I have been reading on an solution approach to PHP5 and MySQL by Apress, good book too I advise it I really do!): Here's a simple example of not really a proper class but objects in PHP alone, without delcaring a class (not advised in a real system obviously): <?php $ball->colour = "Green"; $ball->type = "Football"; echo $ball->colour; // Outputs Green echo $ball->weight; // Outputs 100 ?> To make this more formatted but I am not sure how this works, anyone care to explain this printf construct? printf("The ball's colour is %s, and its weight is %d kilos.",$ball->colour, $ball->weight); This will output: The ball's coloor is green, and its weight is 100 kilos. Not sure how the printf function works exactly, never really used it. The most basic way to create objects! Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115082 Share on other sites More sharing options...
j.smith1981 Posted September 24, 2010 Share Posted September 24, 2010 An associated array could be something like: $myArray = array('myKey'=>1, 'anotherKey'=>2) while($element = each($myArray)) { echo "Key is: " . $element['key'] . " - Value is: " . $element['value']; } Not tested though! Thanks so much for all the help people have given me on this site I really truely appreciate it, cant wait for some replies to this would be good to hear someones opposing opinion of my code. I cant believe I am showing others how to do things, even though these are mostly taken from books I have read, but their really making sense, its well cool! Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115094 Share on other sites More sharing options...
craigtolputt Posted September 24, 2010 Share Posted September 24, 2010 I wonder if you can help me?? I need the user to select an option from 6 different drop downs and then i need to check a DB to see if there is a row that contains all the values, if theres is a row with all the values in then i need to return or echo the price value from that row??? do you know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115118 Share on other sites More sharing options...
j.smith1981 Posted September 24, 2010 Share Posted September 24, 2010 I dont know off hand myself. I am just teaching myself object orientated programming but will have a look at a few examples and see what I can come up with. Shouldnt be too difficult though, depending on what you actually are trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115131 Share on other sites More sharing options...
DavidAM Posted September 24, 2010 Share Posted September 24, 2010 @j.smith1981 To make this more formatted but I am not sure how this works, anyone care to explain this printf construct? printf("The ball's colour is %s, and its weight is %d kilos.",$ball->colour, $ball->weight); This will output: The ball's coloor is green, and its weight is 100 kilos. Not sure how the printf function works exactly, never really used it. The most basic way to create objects! Not sure where you got the weight from, since you never assigned it. But printf() is a handy tool for writing PHP code that is easier to read, and does not require as much quote escaping. // Which is easier to read to you? printf("The ball's colour is %s, and its weight is %d kilos.",$ball->colour, $ball->weight); // or echo "The ball's colour is {$ball->colour}, and its weight is {$ball->weight}."; // or echo "The ball's colour is " . $ball->colour . ", and its weight is " . $ball->weight . "."; Anyway it's for printing formatted statements. The first argument is the format, and the remaining arguments are replacement variables. Each percent symbol ( '%' ) in the format is a formatting command which is replaced by the next argument in the list. So, in the example '%s' (the 's' indicates a string) is replaced by the value of $ball->colour; and '%d' (the 'd' stands for integer) is replaced by the value of $ball->weight. The formatting commands can have specifications between the '%' and the type ('s') such as '%20s' which will use (at least) 20 character positions for the value. So, in this example, with '%20s', you would get "is green , and". I use the sister function, sprintf(), to build my sql statements. sprintf() returns the string instead of printing it: $sql = sprintf('SELECT product, description, cost FROM products WHERE product = "%s" AND isActive = 1 AND cost BETWEEN %d AND %d', mysql_real_escape_string($_POST['product'], intval($_POST['lowCost']), intval($_POST['hiCost'])); I can look at that code, and read the complete SQL statement at a glance. I can also see all of the variables going into the statement in one place. I just makes the code cleaner (IMHO). [That code is over simplified and assumes an integer cost, but sprintf does support floating point as well]. I also use printf() and sprintf() for outputting HTML code when I need to include variable values in it. Another nice thing about these functions, is that you can reference the replacement variables by an ordinal number, so you can use the same variable in multiple replacements: $sql = sprintf('SELECT product, description, cost FROM products WHERE ( product = "%1$s" OR description LIKE "%%%1$s%%") AND isActive = 1 AND cost BETWEEN %2$d AND %3$d', mysql_real_escape_string($_POST['product'], intval($_POST['lowCost']), intval($_POST['hiCost'])); Here we used the "product" value in two places, without having to supply it to the funtion twice (and we didn't have to escape it twice - saving valuable CPU cycles). You'll also notice the double-percent signs in the LIKE phrase. That basically escapes the '%' so it is placed into the string literally as a percent sign. So the value of $sql from this code, might be: SELECT product, description, cost FROM products WHERE ( product = "computer" OR description LIKE "%computer%") AND isActive = 1 AND cost BETWEEN 500 AND 900 Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115244 Share on other sites More sharing options...
ignace Posted September 25, 2010 Share Posted September 25, 2010 1. The first line displays: h3 Wrong. Explanation: reference #1 If we look closely at this page we see that the operators + - . have a left associativity which means it's executed from left-to-right. In the expression 'h' . 1 + 2 . is executed first becoming 'h1' + 2. reference #2 If we look at this page we see that an operation as [string + integer] integer is preferred and the string is converted to an integer (0) translating the statement 'h1' + 2 to 0 + 2. 2. The second line was hard. I had to Google for documentation, apparently it is a 'ternary operator'. It displays: foo - first it checks if 'true' is true, which it is, so the value 'Hello' is assigned. Then it goes to the next part of the formula. Since 'Hello' has a value of 'true' (it is true as long as it does not hold the value of 0 or the keyword 'false'), the value 'foo' is assigned and finally displayed. The following formula will display 'bar': echo false ? 'hello' : 0 ? 'foo' : 'bar'; // Output: ? Correct. 3. Third one I had to 'cheat', i.e. execute it to see what is does. It checks if 'a' is smaller than 'A' and returns true or false. When true, it returns 1 and '1' is diplayed, otherwise it returns false, in which case nothing is displayed. I guess it's equivalent to 'echo false'. So I suppose lowercase 'a' comes before uppercase 'A'. Correct. 4. The last one dazzled my mind, had to 'cheat' as well. I guess an empty/non-existing array defaults to false? Correct. ------------------ Riddle me this, riddle me that: echo '1h' . 2 + 3 There's a little subtly in this one Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115443 Share on other sites More sharing options...
Darkelve Posted September 25, 2010 Author Share Posted September 25, 2010 echo '1h' . 2 + 3 The above is equal to the following: echo '1h2' + 3 http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion If the string starts with valid numeric data, this will be the value used So 1h2 gets converted to 1 and then 3 is added. So the answer is: 4 Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115479 Share on other sites More sharing options...
ignace Posted September 25, 2010 Share Posted September 25, 2010 echo '1h' . 2 + 3 The above is equal to the following: echo '1h2' + 3 http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion If the string starts with valid numeric data, this will be the value used So 1h2 gets converted to 1 and then 3 is added. So the answer is: 4 That's correct I'm soon to be in need of more difficult riddles Ok, one before going to bed while($var --> 1); echo $var; Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1115610 Share on other sites More sharing options...
Darkelve Posted September 30, 2010 Author Share Posted September 30, 2010 I think this will take a little longer... do not really know where to start. I've seen the arrow ( -> ). Seen it in articles about objects and classes before, so maybe I should read up on that? But I've never seen it with a double dash. Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1117484 Share on other sites More sharing options...
ignace Posted September 30, 2010 Share Posted September 30, 2010 But I've never seen it with a double dash. Hint: --> is not an operator on it's own Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1117551 Share on other sites More sharing options...
j.smith1981 Posted September 30, 2010 Share Posted September 30, 2010 Thats the one thing I never get is the -> sign, why cant I ever remember what its called, suppose it could be called some kind of construct am I wrong or? But say with the 2 colons (: : but without the space, since it keeps putting up an emoticon grrr!) thats like -> but when your assigning an object with data, but the data itself is a constant. I believe the 2nd to be correct though could need a bit of rewording, I tend to say that when I am not 100% sure what I am talking about, since this stuff has only just come to get stuck in my head very lately, only now am I able to somewhat understand what I am doing for myself. Great discussions though! Gets my theory going I can tell you! Jez. Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1117605 Share on other sites More sharing options...
ignace Posted September 30, 2010 Share Posted September 30, 2010 But say with the 2 colons (: : but without the space, since it keeps putting up an emoticon grrr!) thats like -> but when your assigning an object with data, but the data itself is a constant. :: is called Scope Resolution operator or Paamayim Nekudotayim (Hebrew for double colon) and allows you to act upon static methods/constants/variables. The Object operator (->) allows you to call public non-static methods/variables. For those in need of another challenge: $a = 0; echo ~$a; // Output: ? Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1117637 Share on other sites More sharing options...
ignace Posted September 30, 2010 Share Posted September 30, 2010 For those in need of more challenge(s): $array = array(1, 2, 3); foreach($array as $k => &$v); foreach($array as $k => $v); print_r($array); // Output: ? Quote Link to comment https://forums.phpfreaks.com/topic/214171-could-anyone-give-me-some-exercides-challenges/#findComment-1117641 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.