Welcome! Log In Create A New Profile


Scope of my Variable

Posted by 42609852 
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
Scope of my Variable
July 11, 2013 11:05PM
Ok not sure how to get this working...
x is at position 5 and it finds it correctly inside the loop but looses the value outside the loop...
my guess is its got to do with the variable scope...
but tried global and GLOBAL["Answer"]
and neither seems to work... any clues where I can look to find solution

Language: PHP
$string="this x is what I am looking for"; $Answer=strlen($string); $myAray = array("z","x","t"); foreach($myAray as $Item){   $ItemFind = strpos($string,$Item); echo $ItemFind ; //echo "window.alert($Answer)"; if($Answer>$ItemFind){ $Answer =$ItemFind; echo $Answer; } } $Answer =$Answer+1; echo $Answer
Re: Scope of my Variable
July 12, 2013 08:48AM
Is this for the array assignment page?
Re: Scope of my Variable
July 12, 2013 09:07AM
Hey Marius.
Good to see you also taking this smile

Nope this is a string function I am trying to get right....
the function works fine...
the array and the loop works fine. (you can see this by looking at the echo inside the loop)

but the echo after the loop for some reason makes the $Answer 0 (+1)
my best guess is the scope of the variable... but checked the net and tried a few things but nothing as of yet...

I will keep going though.
(I prob asked to quick and should bash my head a little more first smile )
Re: Scope of my Variable
July 12, 2013 09:41AM
From the top of my head - foreach does not have scope - it will exist outside. The problem is

Language: PHP

It exists in a loop, so it runs through the other conditions as well, and is set to zero when it is not larger. Remove the if statement - you can use it outside.
Re: Scope of my Variable
July 12, 2013 01:14PM
Thanks Mac I will go try it...

I think I need the if statement in the loop to change my Answer...
the goal is to find the first vowel in a string so if it is only after the loop....
I think it wont be checking all the vowels...

Hmmm... but that said I "THINK" I just clicked what you mean...
when the vowel is not found then the $ItemFind is set to 0 and then it will def be < $Answer....

(Sorry to say but if that is the case that is really bad logic from people that wrote that string function.... should rather return Null or False....what hapens if there is a vowel in position 0.....i.e $String = "Emerald"winking smiley
(Hahaha.... sorry have to laugh at myself trying to critisizing people thats so smart they developed a programming language.... I might be getting over confident with the little knowledge that I have smiling bouncing smiley )
well guess I will go play around with the code when I get home.

Ok thanks for that
Re: Scope of my Variable
July 12, 2013 02:08PM
Yes, not all code is good. My suggestion is perhaps not the best either.... but as this query falls outside the course, I stop here....!
Re: Scope of my Variable
July 12, 2013 05:35PM
holy moses...I got it...damn so stupid I could just roll over and give up on life...
ok maybe that is a bit too dramatic...

was a double prob...
firstly i added this
Language: PHP
if(strpos($string,$leter)!=false){ }

to make sure it only ran the original if statement if it has found a particular vowel
secondy (I did not include all my code and another problem was when trying to add text and an int.....
like so

Language: PHP
$Answer = "The first leter is seen in position: ".$Answer +1;

it totally freaked out and always only returned one...why...well not 100% sure
but by changing it to
Language: PHP
$Answer = "The first leter from list is seen in position: ".($Answer +1);

it worked

if the leter is siting at position 1 then the strpos returns 0 and 0 in an if statement returns false
so it never steps into the other if statment to update the
had to hack it by adding a leter to front of $string
sorry for wasting anyones time but thanks for trying to help.
Re: Scope of my Variable
July 22, 2013 01:36PM
@42609852 I see you got your problem solved, well done. thumbs up
Sorry, only registered users may post in this forum.

Click here to login