We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Why am I getting this; php
Topic Started: Nov 14 2005, 12:32 AM (530 Views)
dbzlotrfan
Member
[ *  *  *  *  *  *  * ]
<?PHP
Function some_thing () {
$num = 18;
}
echo Function some_thing;
?>

is the code, and the error:
Parse error: parse error, unexpected T_FUNCTION in C:\Program Files\xampp\htdocs\PHP_BOOK\PHP\What.php on line 5



I know it's probably not the right function syntax. but it's a try. :P
Offline Profile Quote Post Goto Top
 
Jory
Member Avatar
Member
[ *  *  *  *  *  * ]
Well.. because its all wrong. :P

Code:
 
<?PHP
function some_thing () {
$num = 18;
return $num;
}
some_thing();
?>

Would work.
Code:
 
<?PHP
function some_thing () {
$num = 18;
return $num;
}
$var = some_thing();
echo $var;
?>

Would also work.
Code:
 
<?PHP
function some_thing () {
echo 18;
}
some_thing();
?>

Is a third option.

To make a function give a result, you must eigher tell the function itself to echo it, or return it, put it in a var and echo that.

Now, here is something that might work, but I'm not sure about it.
Code:
 
<?PHP
function some_thing () {
return 18;
}
echo some_thing();
?>

Offline Profile Quote Post Goto Top
 
dbzlotrfan
Member
[ *  *  *  *  *  *  * ]
Your first, didn't work. Just got a blank page.
Offline Profile Quote Post Goto Top
 
Dennis
Member Avatar
Member
[ *  *  *  *  *  *  * ]
What are you trying to do?

I think the correct syntax to echo 18 would be this:

Quote:
 
<?php

function something() {
    $num = 18;
    echo $num;
}

?>


Qj almost got it right. You need an echo, not a return. The return is for something different.
Offline Profile Quote Post Goto Top
 
solinent
Member Avatar
Member
[ *  *  * ]
<?php
Function return_18() {
return 18;
}
echo return_18();
?>

^^ Will work.
Offline Profile Quote Post Goto Top
 
Jory
Member Avatar
Member
[ *  *  *  *  *  * ]
Dennis
November 15, 2005 01:31 AM
Qj almost got it right. You need an echo, not a return. The return is for something different.

>_<
I ur teh stupid.
Offline Profile Quote Post Goto Top
 
Seth
Member Avatar
I has a pony
[ *  *  *  *  *  *  *  *  * ]
No, your examples are much more valid than Dennis', even though you mixed up a call.

Functions should NEVER output data as a result of running them. They should ONLY return data. It is very bad programming practice to have a function output. Have it return a result and then you can handle it how you need to.

Functions exist only as black boxes to put data into and get results out of. Nothing more.
Offline Profile Quote Post Goto Top
 
Dennis
Member Avatar
Member
[ *  *  *  *  *  *  * ]
>,<

*Dennis writes it down.

Sorry.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply