Web Develop Forum: Lesson 1 - Homework - Web Develop Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lesson 1 - Homework Studying string's

#1 User is offline   Jack Icon

  • Member
  • PipPip
  • Group: Teacher
  • Posts: 25
  • Joined: 31-January 09
  • Location:Australia

Post icon  Posted 03 February 2009 - 11:38 AM

Hi,

Righto - so I wanted to clear a few things up after the lesson. Firstly, heres how my current lesson timing runs (it's based on my local time with AEST (gmt+1100)). Monday evening I'll post the lesson for that week. Tuesday evening (now) I'll post the actual homework for that week. That gives roughly 24 hours to let me know if you have got any questions or issues with the lesson. In the lessons I'll cover the basics of what you need to know ( e.g. data types).

Home work is designed to not only "teach" you how to do things, but where to find the information if you're not able to post on forums, or on IRC etc. Often it'll be finding the exception to a rule, or listing more options (other unused options etc) than what I've explained. Although you should be able to know PHP fairly well without completeing the homework, I really recommend you do it because it will help your understanding, and it will be that little thing that makes things click in your brain.

________________________________________________________

So - To lesson 1's homework.

In the lesson yesterday, I covered the basics of data, and variables. Below are 10 questions to get you understanding the specifics of strings, and other data types.


1) List 4 different ways to create and/or display a string.
2) What is the output from the following code? Suggest an example where it might be useful to use that method.
CODE
<?php
$variable = "Hello!";
$name ="variable";
echo $$name;
?>

3) What does the following code do/display, and why it might be useful? (This is a very important feature of PHP).
CODE
<?php
$foo = 3.14159;
echo "4 cows" + $foo;
?>

4) Create 2 variables, and find a way to join them together to make one string, save it in a variable and then display it all in uppercase. (hint: look into using PHP functions, though we haven't covered function use so its a "bonus mark").
5) Why doesn't the following code work, and how can I fix it?
CODE
<?php
echo "\\";    //Hint,  I want the output to be 2 backslashes one after the other
?>

6) Explain what the following data type is, and how I can make it a number (integer).
CODE
<?php
echo 0x1B;
?>

7) Why doesn't the following code round the number as expected?
CODE
<?php
echo (int) 4.9;  // Expected output is "5"
?>

8) What do the following data definitions have in common? Note that if you type this into a PHP page, it will cause an error ( its not 100% correct code).
CODE
<?php
"string"
1
true
-1
?>

9) What is a NULL value and when/why is it used?
10) Is this homework too hard, and did you want it to be easier tongue.gif?

That's it. For now, I'll suggest that you look around on the PHP website, in the documentation pages....

As I mentioned above, I'm not at all expecting you to complete all the homework, nor am I going to ignore it. If you do want to do it or some of it, I'll go through it and "mark" it, and help each of you individually in explaining the answers to anything you couldn't find etc. If anyone feels protective of their code etc (i.e., you don't want to post a copy of your work on the forum) you can email me the code when its complete etc. Please post on the forum when you have emailed me the homework so I can make sure it doesn't go to spam etc. Because of the above reason for homework, I'm not setting a due date as such, but I reccomend you look at it if you are going to, before the next lesson (because just like maths, each lesson will build on the previous). jack@battleages.com is my email - so pretty much do it, send it, ill get back to you and we can get working !!! Just a note though, if you could send the answers as the email body and not as an attachemt (.doc .pdf etc) then that would help a lot. It's easiest for me because I can reply back with "edits" to the code immediately without having to download, open, edit, save etc wink.gif


As always, I'm available on msn or through email - but please post here if you have any questions regarding the homework.

Thanks and happy researching,
Jack

This post has been edited by Jack: 03 February 2009 - 08:46 PM

0

#2 User is offline   Jack Icon

  • Member
  • PipPip
  • Group: Teacher
  • Posts: 25
  • Joined: 31-January 09
  • Location:Australia

Post icon  Posted 08 February 2009 - 12:02 PM

ANSWERS


This post contains the answers to the above homework. If you have any questions or queries about the answers etc. then as always you can email me at jack@battleages.com or reply to this thread.



1)
CODE
"Double quote"

CODE
'Single Quote'

CODE
<<<EOT
heredoc
EOT;

CODE
<<<'EOT'
nowdoc
EOT;

A nowdoc string and a single quote string does not "parse" any variables... it is quote "literal". Most code uses single and double quoted strings, usually for consistency.


2)
The output is "Hello!". This is because the $$name implies that it is the variable called name, thus the value of the"inner" variable is the name for the actual variable called. Sometimes ( though rarely) you might want to be able to input a variable name in a form, in order to print its value in the script. This method could be used, however it does introduce a big(BIG!) security hazard, so in most cases it should only be used on a private not-live site, that your just testing and debugging.

3)
The output is '7.14159' (as a float not a string). This is because of the way PHP handles data types internally. When it reads the string, it first sees the numerical value, and seeing as it is adding a number to a string, it finds as much of the string to use as the number as possible, and "throws away" the rest of the string.

4)
This one was a biggy - you'll use this sort of thing a LOT!. Here you go:
CODE
$var1="Hello ";
$var2="World!";
echo strtoupper($var1.$var2);

Simply put, $variable.$variable implies that they two strings are added together, with the first, then the next - reading left to right. the strtoupper() function accepts a string value and returns the uppercase version of that string. We'll cover more on functions, arguments and return values soon, however you should be aware that this method/use is called "string concatenation" (Thanks to peter, who reminded me that I forgot to mention this smile.gif).

5)
The backslash is used to escape characters, such as new lines, carrage returns and tabs (to name a few). The string "\\" is actually escaping the backslash itself, thus, as the "\\" is a literal 'singl'e character, you need to use the string "\\\\" to get the 2 backslashes after each other. To use an excape character eg newline, use: "\n". to get the actual string '\n', you must use the code: "\\n".

6)
That is the hexidecimal number for 27. To convert it to an integer value, you use the function intval(); eg: intval(0x1b);. This also works for strings etc: intval('198'). Again Peter corrected me on this - you dont actually need to use intval() on a hexedecimal number, because of the way that PHP handles its data types. However do be aware that a hex number is still a number, so treat it as one. I wont be covering much on different number types ( hex, oct, etc) however be aware that they are there.... don't get too confused ( like I did).

7)
PHP uses a "round to zero" method when its casting data. When you change one peice of data into a different type, its using a method called casting. So when you cast somethign to an integer, it essentially "forgets" or "drops" the decimal point. If you wanted to round the number to 5 (which is what we did want) then you would use the function round($int);, which ( in the words of peter) does exactly what it says on the box.

8)
This was a bit of a trick question, you woudl have had to look around for it on the php data types reference, and it would have been found in the boolean values ( if I remember correctly). All of these values, will return True when converted to a boolean value (remember boolean is a True or False value). The only real values that return False are 0 and NULL(which is the answer to the next question wink.gif).

9)
A NULL value is quite litereally a "nothing" value. Its sort of a placeholder to tell us that nothing is there, so we can use it to tell us if a value has been removed from the variable, or we havent set a value to the variable.

10)
I'm assuming that it was too hard ( sorry) - I havent had any replies, answers or any feedback on it. PLEASE! let me know if there is anything you want me to change.



Well thats it! I'll be posting a new lesson tomorrow evening, around this time and the homework 24 hours after that.

Thanks,
Jack


Edit: Peter emailed me with a few things that I had made mistakes on, namely questions 2,3,4,6,7. Please read back over those if you read this post earlier. I'd like to apologise on teh mistakes - I havent been able to test the code samples myself because my server died this week though its working again now - so no more code errors wink.gif ( hopefully).... Thanks Peter!!!!!
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users