A simple php code not working why?

Hello guys,

why the following code not working ?

    <?php 
$friends = array("ammm","monnn","sammm");
$online = '1';
function whereMyFriends(){ 
 if(isset($online)){
	 foreach($friends as $value){echo "Your friends".ucfirst($value)."are online"; }		
	 } else { echo "Your friends are not online"; };
}

//echo "hello world";
echo whereMyFriends();


?>

It is just for learning, so take your time to help me, thanks :wink:

Few things - no need to echo out the function. The function isn’t returning anything. Just call the function and the echos inside of hte function will print to the page.

Also you had an extra semi colon after your else statement. I don’t think you can actually else a foreach loop though? Dunno, I removed it and placed it elsewhere.

I had trouble accessing $online and $friends. Not sure whether that’s a scope issue…shouldn’t have been since they were declared outside of the function…I got it working via this. Perhaps gurus can make this better.

<?php 
$friends = array("ammm","monnn","sammm");
$online = 1;
function whereMyFriends($f, $o)
{ 
  if(isset($o) && $o>0)
  {
    foreach($f as $value)
    {
      echo "Your friends ".ucfirst($value)." are online<br>";
    }
  }
  else
  {
    echo "Your friends are not online";
  }
}
whereMyFriends($friends, $online);
?>

Because to use global variables inside a function, you need to use the global keyword.

But globals are frowned upon. If possible, it’s better to pass arguments, like in Ryan’s example. Though, I’d suggest using more meaningful variable names. Single-letter variables are also frowned upon, except in very small scopes such as $i in a for loop.

Gah that makes so much sense now.

I’m used to Javascript where them just being outside the function, it’s already a global. Thanks Jeff that makes me feel more sane.

Just a quick reply to you both, just to say that I appreciate your support.
I’m reading now your feedbacks and will get back to you for any further question…

Dear Ryan, I read about scope before, but really I don’t understand good this word " scope ", I mean in php, what you mean when you use it here ?
also Jeff said


Dear Jeff, what you did mean by saying “global variables” ? as I know, " globale " is used to bring variable definition from outside the function to inside it, is that correct ?


Thank you guys

This is what I did based on your feedbacks guys, thank you again, and as I’m coding to learn, I think that this code is not better than Ryan’s one, as Jeff said “But globals are frowned upon”.

Also, is that correct to use <div> instead of <br> ? I’m gonna to search about the difference between <br> and </br> and <br />

<?php
// this is just an exemple
$friends = array("ammm","monnn","sammm");
// thi is also an exemple, just to say that $online is set
$online = '';
/*The following code will work without function, but I'm using the
 function here just to learn, also I like the way we are using 
 later the functions in a separate file, and call them by the statement include_once */
function whereIsMyFriends(){ 
   global $friends, $online ; 
   if(isset($online)){
	 foreach($friends as $value){
		 // I add the <div> instead of <br>, as the both give the same result.
	 echo '<div>'."Your friend ".ucfirst($value)." is online"."</div>"; }		
   }
   else { echo "Your friends are not online"; }
}

whereIsMyFriends(); 

?>

You should probably always use the HTML5 doctype.

<!doctype html>

That being said, use <br>. HTML5 doesn’t require self closingtags.

The </br> is invalid. The <br /> is unneeded in HTML5.

Imagine your PHP file as a big building.
And functions in that file are rooms in that building.
And variables are persons who live in that building.

So, when you’re in the room you can see only people in that room, but not people in the corridor or other rooms. That means you’re in a local scope. When you’re in the corridor you can see people here and also you can go to any room of the building. You’re in a global scope now.

Back to your code, variables $friends and $online are in the global scope (in the corridor). If you want to use them in function local scope (in your room) you have to pass them to function (invite them to room) or use global keyword (shout loudly from your room so they will hear you in the corridor).

I hope that explanation helped. And sorry for my grammar, i’m working on it :slight_smile:

Thanks Ryan, this is mean with <br> there is no problem over the internet now as I understand…so we are all using html5

What can I say megazoid, I really appreciate your way to explain the things, thanks !

I’m
inside your building now :slight_smile: and understand better the things. By the
way, and as you imagine the things (I understand better with a visual example ). How we should think when we want to
build a new php project ? ( based on your explanation … )

( NB: don’t forget that I’m still learning PHP now, so the concept is still blurred for me)

PHP
is a world, something like a big GAME, and as you said, when I have a project, is like I
want to construct a building, for that I should prepare the map, and
this is what I like to know, and I think you are the right person who
can explain that…HOW WE SHOULD PREPARE THIS MAP ?

So why I
asked this question ? During my learning ( on lynda
website + sitePoint book ) I didn’t found till now the way, or the
visual look, I mean how I should think or do…create a folder for admin
and other for members, other for CSS
and so on …I mean if functions the rooms, should I put them all in a
folder ? maybe you will tell me it is early to think about that
now, or it is depends the project, or you can use or learn OOP later to
be able to use frameworks later…anyway I still miss the immagination
in this part.

Sorry to bother you with my
question(s) and there is now problem at all with your grammar, I think I have your level not more :slight_smile:

You’re right, it’s always depends on the project and goal you’re trying to achieve.
For example, if you want to create simple contact form then there is no need to use functions at all.
On the other side if you’re going to build massive application then you’ll prefer to use classes, inheritance and other OOP stuff.

I can recommend this book. It covers many of your questions about PHP applications design.

It’s all about practice, don’t worry too much about it now. Just continue to make simple projects, then you’ll see how you can make them more complex.

Thank you megazoid, I appreciate your help and your suggestion concerning the book.
Regards !

If you use XHTML5 instead then it is either <br></br> or <br/> as XHTML requires tags to be closed. You can tell the differrence between HTML5 and XHTML5 by the MIME type and the fact that XHTML5 doesn’t use a doctype at all.

Thank you felgall for your participation and your feedback.

In fact and to be honest, at first moment I didn’t understand all what you explain about the using of <br>, like :

So, I did some search over the web and finally I got you. I think the following page includes what you was trying to explain :

http://www.w3schools.com/tags/tag_br.asp

The result, I will use <br> from now :wink:

I was explaining where <br/> is supposed to be used.

HTML has a MIME type of text/html and expects <br> so any / will be considered to be an error and will be discarded (as HTML is forgiving of errors like that

XHTML has a MIME type of application/xhtml+xml and expects all tags to be closed either using a separate closing tag <br></br> or where the tag has no content by making it self closing <br/> - any errors are supposed to stop the page from displaying past the error so that the error can be corrected before loading it to the web.

The latest version of HTML is HTML 5 which has two modes - standard mode where the code starts with <!doctype html> and quirks mode when it doesn’t start with that tag.

The lastest version of XHTML is XHTML 5 and as XHTML only has one mode it doesn’t need a tag to select the mode.

Older versions of HTML and XHTML were based on SGML and so had an SGML <!DOCTYPE> tag at the top to indicate which version of the standard they were using. As version 5 is no longer based on SGML, pages using that version of (X)HTML no longer have that tag. The HTML5 mode tag looks like the SGML tag but is actually an HTML tag and not an SGML tag.

Thank you felgall, I really appreciate your efforts to explain this point, the vision now is more clear. But, as I don’t have 5% of your knowledge in this issue, I was looking for “a general way” or “the current version” to do the things when I asked my question.

Exemple : As I’m learning now PHP, I want to work on a website with php and MySQL just to apply what I’m learning now. For that reason, I was looking to know what I should chose by default ?.. I think there is also a “general MIME type”, something like a copy/past a ready MIME code…I don’t know if I explain good my situation. When you focus on a programming language, it is better to not diverge and learn other things in the same time ( JavaScript, Ajax, html high level,…) …like for example, I need to insert a JavaScript code, the best way is to take the default one ( something ready over the web), and go ahead with my php learning, to avoid wasting time, and when I finish php, I begin with html5, or xml…

Anyway, I don’t know if I have the best way to learn, but I’m sure that I have the chance to get your support, and the support of Ryan, Jeff and megazoid. Thank you all again and again…

If the filename has a .htm or .html or .php extension then the default MIME type is almost certainly - text/html

With XHTML you need either a .xhtml extension or to manually set the MIME type in PHP.

1 Like

Perfect, this is what I was looking for. Thank you felgall :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.