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.
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 ?
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();
?>
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
What can I say megazoid, I really appreciate your way to explain the things, thanks !
Iâm
inside your building now 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
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.
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.
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âŚ