Jump to content

Could anyone give me some exercides / challenges?


Darkelve

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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: ?

Link to comment
Share on other sites

^^^

 

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

@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

 

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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 :D

 

while($var --> 1);
    echo $var;

Link to comment
Share on other sites

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. :D

Link to comment
Share on other sites

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: ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.