Welcome! Log In Create A New Profile

Advanced

Chapter 2 - Variables. A twist of the exercise

Posted by witbooie 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Chapter 2 - Variables. A twist of the exercise
February 14, 2013 11:02PM
<?php


$num1 = 10;
$num2 = 25;

print($num1 + $num2);

?>

Sorry pros. It's been a while. The basics are quite exciting to a novice like me.
Re: Chapter 2 - Variables. A twist of the exercise
February 15, 2013 05:16AM
LoLsmile

It is pretty exciting isn't it!! spinning smiley sticking its tongue out

Don't worry just to make sure I understand every word everyone says I go google & WikiPedia it, that
way I have more than an Idea what the smart people are talking about, I actual I starting to now.
Another big help is Youtube, it has lots of tutoruals on PHP for beginners, and what makes it nice is that the videos are
short & to the point.
avatar
Mac
Re: Chapter 2 - Variables. A twist of the exercise
February 15, 2013 08:31AM
P.L.E.A.S.E. use formatted code when posting code....
Re: Chapter 2 - Variables. A twist of the exercise
February 15, 2013 02:39PM
Should we post the exercises here?
Thank you
avatar
Mac
Re: Chapter 2 - Variables. A twist of the exercise
February 18, 2013 07:40AM
Nope - as the tut letter points out you report on your experience with them and add knowledge if you can.
Re: Chapter 2 - Variables. A twist of the exercise
February 20, 2013 09:19PM
Good day
i have done the chapter 2 and it was very nice and easy smiling smiley
Re: Chapter 2 - Variables. A twist of the exercise
February 25, 2013 03:16PM
Has Php got a way of rounding of to specific decimal figures? e.g pi or any any number i would want in a form like eg 8.25469..... to 4 decimal figures is 8.2547.......hw cn php do that?......andrew
avatar Re: Chapter 2 - Variables. A twist of the exercise
February 25, 2013 03:48PM
ceil(your number) returns a number rounded upward to the closest integer.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
avatar
Mac
Re: Chapter 2 - Variables. A twist of the exercise
February 25, 2013 04:38PM
Your question may seem insignificant... but it is not, because you are really asking... is there an easier way to do stuff than my way? Good on you for asking it, and on Shado to respond so eloquently smiling smiley

Yes, there is. In-built PHP Functions (the function which is built-in to the PHP parser ask you for your input/s, and provide you with the result without you having to understand the code inside the function . Classes, same usefulness. Written by other people which you adapt to suit your purposes. But baby steps - do not run ahead of yourself. Learn the basics, because you need these baby steps in order to understand how to use functions and classes to your benefit.

The portfolio has been designed to get you to ask such questions.
Re: Chapter 2 - Variables. A twist of the exercise
February 26, 2013 04:50PM
Hi Mac.

I was wondering if there is a way to have a more cleaner variable in PHP like with C++ and C# ?

Where you declare your type of var instead of just adding different variables left, right and center ?

For instance: int, float, string, char ...

Student Number: 78042879 (Part Time Courses)
Re: Chapter 2 - Variables. A twist of the exercise
February 26, 2013 10:43PM
mmmmm interesting question Anton...

in my little experience with php, you do not specify var types eg int float ect. but you can declare a variable and use it.

eg. $cell = $_POST['cell'] or $cell = '082number'; yu can however validate eg your form to accept either numbers only or text only.
avatar
Mac
Re: Chapter 2 - Variables. A twist of the exercise
February 27, 2013 08:01AM
PHP is weakly typed. In C a variable is declared to hold a type. In PHP the type of variable is determined by the value assigned to it.

Language: PHP
$value=0; //because you typed a zero, the variable is now an integer type variable. $amount=0.00; //this is now a float $amount=';Arrears"; //now it is a string

When you work with db's, you will define a column that you will store integers in as an INT (e.g. INT, BIGINT etc.) or if string, as TEXT.
Re: Chapter 2 - Variables. A twist of the exercise
February 27, 2013 10:38AM
thanks, always wondered about about data types in php

..&ru..
Re: Chapter 2 - Variables. A twist of the exercise
February 27, 2013 09:22PM
Thank Guys. Thank you Mac.

"When you work with db's, you will define a column that you will store integers in as an INT (e.g. INT, BIGINT etc.) or if string, as TEXT."

Yes I notice that when I played with MySQL last month. Also when you setup the "tables" => "content types" in drupal you need to declare what each field's type is.

Student Number: 78042879 (Part Time Courses)
Re: Chapter 2 - Variables. A twist of the exercise
March 20, 2013 09:55PM
html>
<head>
<title>More on Variables - a confusing example</title>
</head>
<body>


<?php
// Testing Variables
$first_number=200;
$second_number=10;
$third_number="200";
$fourth_number="10";
$fifth_number='200';
$sixth_number='10';
print($first_number+$second_number." "winking smiley;
print($third_number+$fourth_number." "winking smiley;
print($fifth_number+$sixth_number);
?>

</body>
</html>

I was expecting my fifth and sixth number to print out 20010 but all three print statements printed out 210. I thought the single quotation marks would make it text. What am I missing?
avatar Re: Chapter 2 - Variables. A twist of the exercise
March 21, 2013 05:25AM
Hi there AngelaWoodend-77672860,
As Mac explained php is not strongly typed, and if I interpret this correctly, along with the info in the php manual, strings are evaluated as integer or float (depending on the size of the string) if they are used in a numeric context, as explained in the
php manual here: http://www.php.net/manual/en/language.types.type-juggling.php
and more specifically this page: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

Like in their example in the manual:
Language: PHP
$foo = "0"; // $foo is string (ASCII 48) .... $foo = 5 + "10 Little Piggies"; // $foo is integer (15) ...

Since you are using the plus sign in your code:
Language: PHP
print($fifth_number+$sixth_number);
Php interprets the variables as integers, because + creates a numerical context, although you declared the variables as strings earlier on.

Hope this helps a bit.
Cheers.
Re: Chapter 2 - Variables. A twist of the exercise
March 21, 2013 02:39PM
Playing around with the plus sign, strings, numbers as strings and concatenations:

Language: PHP
<html> <head> <title> Numbers and Strings </title> </head> <body> <?php   $String10 = ';10';; $String20 = ';20';; $StringHat= ';hat';; $PlusTotal= print($String10 + $String20.';</br>';); //prints 30 $PlusTotal= print($String10 + $String20 + $StringHat.';</br>';);//prints 30 and disregards $StringHat $ConcatenateTotal = print($String10.$String20.$StringHat.';</br>';);//prints 1020hat     ?> </body> </html>
Re: Chapter 2 - Variables. A twist of the exercise
April 02, 2013 12:41AM
This chapter was so nice i wish there was much of it to do
Sorry, only registered users may post in this forum.

Click here to login