Welcome! Log In Create A New Profile

Advanced

Section 3 - Conditional Logic

Posted by 78044545CharlesU 
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
Section 3 - Conditional Logic
July 05, 2013 08:32PM
Hi, haven't finished with this section yet, still getting to know how to use The Switch Statement. Had some fun
changing images. Should be useful for working with Forms
Re: Section 3 - Conditional Logic
July 07, 2013 01:51PM
I've completed this section and understand if, if_else, if_else if statements. Took me a while to understand in what different practical situation you would use a Switch statement instead of the aforementioned statements. Comparison and logical operators understood, hopefully more regular use will help me to memorize them.
Re: Section 3 - Conditional Logic
July 09, 2013 09:28PM
I'm still busy on this section. Trail and error to understand this section a little bit better. Will most probably finish this section tomorrow
Re: Section 3 - Conditional Logic
July 10, 2013 09:02PM
I finished this section. I'm struggling with Operator Precedence. Will come back to it at a later stage.

M
Re: Section 3 - Conditional Logic
July 12, 2013 02:49PM
I have completed Section 3: Conditional Logic and have understood it fully.

Interestingly, for the < (less than) and > (greater than) operators where the variables are not

values, but strings/characters/words, PHP will evaluate those logical operators in an if/else statement by counting

the number of characters in each variable and making a descision based on it.

e.g
Language: PHP
<?php   $correct_username = ';logmein';; $what_visitor_typed = ';logmein2';;   if ($what_visitor_typed > $correct_username) { print("You';re not a valid user of this site!");   } else { print("Welcome back, friend!"); } ?>

will print out: You're not a valid user of this site!

Dr Mac, please confirm if this is the case.

S
avatar
Mac
Re: Section 3 - Conditional Logic
July 12, 2013 03:41PM
Huh?

Language: PHP
if ($what_visitor_typed == $correct_username) //is tha same //or if ($what_visitor_typed != $correct_username) //is not the same

There are various in-built functions to test the number of characters (e.g. strlen) for other purposes.... but why do you want to caompare the number of characters, because a username of 'abcd' is not equal to 'paul'
Re: Section 3 - Conditional Logic
July 12, 2013 04:59PM
I have completed this section, think I have a good basic understanding of Conditional Logic for now.
avatar Re: Section 3 - Conditional Logic
July 12, 2013 05:48PM
I have come back to this section as I discovered I wasn't as clear on this chapter as I should be.
Question deleted
avatar Re: Section 3 - Conditional Logic
July 13, 2013 01:30PM
Oh my God its so easy to print images in PHP than it is in java script..spinning smiley sticking its tongue out
avatar Re: Section 3 - Conditional Logic
July 13, 2013 02:14PM
Guys I noticed there is one operator not mentioned in the homeandlearn website and that is the "===" its the same as the "==" operator except that it also check for datatype..
i.e (consider the following code)
Language: PHP
$myName = Sipho; $entryName = Sipho; if ($myName === $entryName){ echo $myName." is the same as ". $entryName." and they both have a datatype of " .is_int($myName); };

I hope this makes sence..
avatar Re: Section 3 - Conditional Logic
July 14, 2013 03:50PM
Hi Sipho
That operator is mentioned along with !==
These are mentioned on the last page of section 3 under the operator Precedence list. as follows:

The only operators you haven't yet met on the list above are the = = = and != = operators.

In recent editions of PHP, two new operators have been introduced: the triple equals sign ( = = =) and an exclamation, double equals ( != =). These are used to test if one value has the same as another AND are of the same type. An example would be:

$number = 3;
$text = 'three';

if ($number = = = $text) {
print("Same"winking smiley;
}
else {
print("Not the same"winking smiley;
}

So this asks, "Do the variables match exactly?" Since one is text and the other is a number, the answer is "no", or false. We won't be using these operators much, if at all!

Best regards
Susan
Re: Section 3 - Conditional Logic
July 14, 2013 06:19PM
Section 3 completed and moving to the next one. I find the if..else, if..else..if statement very interesting and easy to work with.
avatar Re: Section 3 - Conditional Logic
July 15, 2013 09:11AM
Thanks fo elaborating there Susan, I've seen what you mean.. spinning smiley sticking its tongue out
avatar Re: Section 3 - Conditional Logic
July 15, 2013 04:43PM
Please make use of formated code..
Re: Section 3 - Conditional Logic
July 17, 2013 03:50PM
@Dr Mac,

What I was trying to see (from my question above) is what the resultant behavior would be if you use value operands (such as < and >winking smiley on variables that are not numbers (strings). I'm just messing around with PHP really smiling smiley

Anyway, what I did discover that PHP actually does an automatic conversion of a string to a number (if it can) without you having to first use a function.

e.g

Language: PHP
<?php   $variable1 = ';5';; //string variable $variable2 = 6; //integer variable   print($variable1 + $variable2);   ?>


will print out 11 smiling smiley

i can see much use for this.

S
Re: Section 3 - Conditional Logic
July 17, 2013 10:32PM
Just completed this section, keen to get building stuff.

@SK-77478452 I like that you're messing around with stuff like that. But I would have expected PHP to print "7" because $variable1 has a length of 1. Seems to me that if PHP is asked to add a string that starts with a number to an int or another string that starts with a number, it just adds the numbers the strings start with and totally ignores the length, eg.

Language: PHP
$variable1 = ';9aasd asd';; $variable2 = ';6saf sdfjh';;   print($variable1 + $variable2);

prints "15"
But

Language: PHP
$variable1 = ';9aasd asd';; $variable2 = ';aasfafafsdaf6 sdfjh';;

Prints "9"
Don't think there's any real use for that... But interesting still.
Re: Section 3 - Conditional Logic
July 17, 2013 11:21PM
Section 3 completed and understood! thumbs up
An important section as these are the tools one will use most when programming!
Re: Section 3 - Conditional Logic
July 18, 2013 03:09PM
Re: Section 3 - Conditional Logic
July 20, 2013 05:34PM
Just finished section 3.

I think I understand most of it. But might have to refer back to this section once I start with the project.

In some cases I feel that the exercises don't always show exactly how the operators work, but I believe everything will make better sense later on.
Re: Section 3 - Conditional Logic
July 22, 2013 12:13PM
Completed and understand section 3.

I found if you dont understand something, then just search the internet for more examples. www.w3schools.com is a nice site to help you understand better.
Re: Section 3 - Conditional Logic
July 22, 2013 08:05PM
completed
Re: Section 3 - Conditional Logic
July 24, 2013 01:30PM
Completed this section and I understand it.

Will have to revise it again for better before the I start with the project the "If Statement" was more interesting and I had fun with it.
Re: Section 3 - Conditional Logic
July 24, 2013 10:53PM
I have completed this section and found it very interesting and useful. Although once i opened the suggested selectPicture examples i found some humility in the sense of so much code for something so simple.
Re: Section 3 - Conditional Logic
July 27, 2013 03:49PM
Completed this section and exercises a few days ago
Re: Section 3 - Conditional Logic
August 12, 2013 10:44AM
Section 3: Conditional Logic: Completed, intresting section . Looking foward to next section !
Re: Section 3 - Conditional Logic
August 13, 2013 11:33AM
Completed the section smiling smiley
Re: Section 3 - Conditional Logic
August 13, 2013 12:57PM
Completed - It's all about possibilities!
Re: Section 3 - Conditional Logic
August 14, 2013 03:48PM
Hi, I've completed this section!

I've learned the hard way on how important it is to write down the correct symbols. I mistakenly wrote the "==" sign ("has the same value" sign) as "= =" and got an error message. Saying that there is an error in one of my lines in my editor but couldn't see it at the time. Untill I re-write the whole code from scratch. As seen below.

Language: PHP
<?PHP   $kitten_image = 0; $church_image = 1;   if ($kitten_image == 1) { print ("<IMG SRC =images/kitten.jpg>"); }   if ($church_image == 1) { print ("<IMG SRC =images/church.jpg>"); }   ?>


Just glad I found it because I spent lots of time on something that seems so small. thumbs up
avatar
Mac
Re: Section 3 - Conditional Logic
August 14, 2013 03:54PM
Be prepared to spend a day or more on something small.... it is part and parcel of typo's. Which is why it is sometimes good to have someone else look at the code because one gets so bogged down you miss typo's.
Re: Section 3 - Conditional Logic
August 19, 2013 09:48PM
Just completed this section, may have to revert to explore more logical operators in examples. Chapter 3 in the textbook is very helpful.
avatar Re: Section 3 - Conditional Logic
August 20, 2013 09:33AM
Hi Warren

If you evaluating to figure out if a variable has been initiated (not empty) or contains a value equal to 1, you could do it in two ways.

OPT 1 (like the way you wrote your code above)
Language: PHP
$Kitten_image = 0;   if($Kitten_image == 1){ echo "We have a kitty in here"; } else { echo "Would you believe it, there are no kittens here.."; }

OPT2 (the other way.. I prefer this one coz its shorter)
Language: PHP
$Kitten_image = 0;   if($Kitten_image){ #if variable is initiated or has a value equal to 1 then the following take place echo "We have a kitty in here"; } else { echo "Would you believe it, there are no kittens here.."; }

I hope that makes sense.. lol tongue sticking out smiley
Sorry, only registered users may post in this forum.

Click here to login