How do I simply call a function by using PHP?
Say I have created:
function sam(){
echo "Hello World!;
exit();
}
How would I initiate the function?
How do I simply call a function by using PHP?
Say I have created:
function sam(){
echo "Hello World!;
exit();
}
How would I initiate the function?
Honestly, Sam, you may want to pick up a PHP book and read up some before asking for help on sites like these. This is about as basic as it gets.
@Gumbo: I edited out the " you added to his code; if that's how he posted it and there's answers referring to the missing quote, then it takes everything out of context to fix the mistake in the question.
I don't see any problem asking extremely basic questions here. I don't think we should discriminate against beginners here.
This site is for programming questions, not necessarily 'advanced' programming question. Sam's question was on topic and I don't think it should be downvoted because it's an easy answer.
It is a big problem. This question have their answer everywhere in the web, so you don't need another place to be. At the contrary, SO allows you to solve problem you have usually trouble to deal with or to discover things. If you let questions like this one to be asked, then you will add a lot of noise in SO and the interesting stuff will be lost in the mess (while the basic stuff won't be best answered as anywhere else). Finally SO would become as interesting as any fora in the Web.
I didn't downvote this and I'm all for beginner questions here – I just got done defending another one of his questions when someone said it was too simple – my advice was more towards his own personal journey towards being a better programmer. Coming here to ask questions like these displays a severe lack of understanding basic programming concepts. In the spirit of that, he's probably best served reading a book to get an idea of how languages work, instead of generating Yet More Bad PHP Code.
Like this –
Well, i suppose:
sam();
Read the PHP manual about functions. And note that
exit
will not just exit the function but the whole script. So nothing else will be executed after this call.Is this a trick question?
First thing I notice is you are missing a quote:
.. should be ..
(I see you fixed your post after I mentioned the above by adding the quote).
Next, in your case, the function echo’s the result, and then exits the entire script (it doesn’t just exit the function as you may be assuming), so not much you can do with that other than this:
However, if your function was more real-world, like this:
.. then you could do any of these (and more) ..
help yourself to php.net
I’ve just started using OOP techniques in PHP, and one simple bit of information that I found hard to find online was that if you want to call your function as part of the object you need to use
$this->functionName();
i.e.
In addition to the other answers here, I think your intention for using exit() is actually satisfied by the return construct. More info on returning values.
However, in your example there is no reason to have it since PHP functions return void automatically once the function ends and no other return value has been specified.
An example where you would want to use return to stop a function from continuing:
I couldn’t tell you what real-world application this particular example has but it shows a useful situation where you may want to cut a function short.
Another example showing that return is not needed in simple functions:
You may also want to checkout the PHP.net manual section on Functions. Hope this helps.