# Section 3 - Conditional Logic

Posted by 78044545CharlesU
Announcements Last Post
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Section 3 - Conditional Logic July 05, 2013 08:32PM Registered: 6 years ago Posts: 123 Rating: 0
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 Registered: 8 years ago Posts: 115 Rating: 0
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 Registered: 6 years ago Posts: 22 Rating: 0
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 Registered: 6 years ago Posts: 22 Rating: 0
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 Registered: 6 years ago Posts: 21 Rating: 0
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

\$what_visitor_typed = 'logmein2';

{
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
 Re: Section 3 - Conditional Logic July 12, 2013 03:41PM Admin Registered: 14 years ago Posts: 9,803 Rating: 332
Huh?

```Language: PHPif (\$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 Registered: 6 years ago Posts: 38 Rating: 0
I have completed this section, think I have a good basic understanding of Conditional Logic for now.
 Re: Section 3 - Conditional Logic July 12, 2013 05:48PM Registered: 6 years ago Posts: 22 Rating: 0
I have come back to this section as I discovered I wasn't as clear on this chapter as I should be.
Question deleted
 Re: Section 3 - Conditional Logic July 13, 2013 01:30PM Registered: 6 years ago Posts: 73 Rating: 0
Oh my God its so easy to print images in PHP than it is in java script..
 Re: Section 3 - Conditional Logic July 13, 2013 02:14PM Registered: 6 years ago Posts: 73 Rating: 0
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..
 Re: Section 3 - Conditional Logic July 14, 2013 03:50PM Registered: 6 years ago Posts: 22 Rating: 0
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";
}
else {
print("Not the same";
}

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 Registered: 6 years ago Posts: 37 Rating: 0
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.
 Re: Section 3 - Conditional Logic July 15, 2013 09:11AM Registered: 6 years ago Posts: 73 Rating: 0
Thanks fo elaborating there Susan, I've seen what you mean..
 Re: Section 3 - Conditional Logic July 15, 2013 04:43PM Registered: 6 years ago Posts: 73 Rating: 0
Please make use of formated code..
 Re: Section 3 - Conditional Logic July 17, 2013 03:50PM Registered: 6 years ago Posts: 21 Rating: 0
@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 > on variables that are not numbers (strings). I'm just messing around with PHP really

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

i can see much use for this.

S
 Re: Section 3 - Conditional Logic July 17, 2013 10:32PM Registered: 6 years ago Posts: 25 Rating: 0
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 Registered: 6 years ago Posts: 37 Rating: 0
Section 3 completed and understood!
An important section as these are the tools one will use most when programming!
 Re: Section 3 - Conditional Logic July 18, 2013 03:09PM Registered: 6 years ago Posts: 68 Rating: 0
.
 Re: Section 3 - Conditional Logic July 20, 2013 05:34PM Registered: 6 years ago Posts: 57 Rating: 0
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 Registered: 6 years ago Posts: 10 Rating: 0
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 Registered: 6 years ago Posts: 22 Rating: 0
completed
 Re: Section 3 - Conditional Logic July 24, 2013 01:30PM Registered: 6 years ago Posts: 39 Rating: 0
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 Registered: 6 years ago Posts: 12 Rating: 0
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 Registered: 6 years ago Posts: 14 Rating: 0
Completed this section and exercises a few days ago
 Re: Section 3 - Conditional Logic August 12, 2013 10:44AM Registered: 8 years ago Posts: 55 Rating: 0
Section 3: Conditional Logic: Completed, intresting section . Looking foward to next section !
 Re: Section 3 - Conditional Logic August 13, 2013 11:33AM Registered: 6 years ago Posts: 15 Rating: 0
Completed the section
 Re: Section 3 - Conditional Logic August 13, 2013 12:57PM Registered: 6 years ago Posts: 11 Rating: 0
Completed - It's all about possibilities!
 Re: Section 3 - Conditional Logic August 14, 2013 03:48PM Registered: 6 years ago Posts: 44 Rating: 0
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.
 Re: Section 3 - Conditional Logic August 14, 2013 03:54PM Admin Registered: 14 years ago Posts: 9,803 Rating: 332
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 Registered: 6 years ago Posts: 14 Rating: 0
Just completed this section, may have to revert to explore more logical operators in examples. Chapter 3 in the textbook is very helpful.
 Re: Section 3 - Conditional Logic August 20, 2013 09:33AM Registered: 6 years ago Posts: 73 Rating: 0
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
Sorry, only registered users may post in this forum.