/    Sign up×
Community /Pin to ProfileBookmark

function; how to create?

I haven’t quite got the hang of functions yet,
I’ve gotten too spoiled of using include() and
placing what I need in a different file.

I’m at a place now where I need everything on one page,
so I need to do a function().

What I’ve tried, I can’t get it to work…

Here is a sample code:

[code=php]

// if count stronger = 0 retrieve the first two items in weaker
if($count_stronger == 0)
{
$weakerpairs = $weaker[0].$weaker[1];
$whichpair = $weakerpairs;
// /////////////////////////////////
// /////////////////////////////////
include(“testpair.php”);
// ////////////////////////////////
// ////////////////////////////////

}

[/code]

a portion of testpair.php is:

[code=php]

<?php
switch($whichpair){
case “EURUSD” : $testpair = “eurusd”; break;
case “USDEUR” : $testpair = “eurusd”; break;
case “EURGBP” : $testpair = “eurgbp”; break;



}
?>

[/code]

I made a few attempts to turn ” testpair.php ” into a function
at the beginning of the code:

[code=php]
<?php
function (testpair){
switch($whichpair){
case “EURUSD” : $testpair = “eurusd”; break;
case “USDEUR” : $testpair = “eurusd”; break;
case “EURGBP” : $testpair = “eurgbp”; break;



}
}
?>

…code…
…code…

// if count stronger = 0 retrieve the first two items in weaker
if($count_stronger == 0)
{
$weakerpairs = $weaker[0].$weaker[1];
$whichpair = $weakerpairs;
// /////////////////////////////////
// /////////////////////////////////
testpair();
// ////////////////////////////////
// ////////////////////////////////

}
[/code]

I’ve tried to navigate through php.net and I can’t seem to find a simple answer.
I have the Sams Teach yourself php and it doesn’t have a simple answer.
Classes, Objects la la la. Gee, all I want is a simple function().

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 13.2010 — [url=http://www.php.net/manual/en/functions.user-defined.php]PHP Manual: User-Defined Functions[/url]

[url=http://devzone.zend.com/node/view/id/637]PHP 101 (part 6): Functionally Yours[/url]

The function's name does not get parenthesized when defining it, the parentheses surround it parameters (a.k.a. arguments), or are left empty if it has no parameters;
[code=php]
function hello()
{
return "Hello, world.";
}

echo "<p>" . hello() . "</p>n";
[/code]
Copy linkTweet thisAlerts:
@anothenauthorSep 13.2010 — [url=http://www.php.net/manual/en/functions.user-defined.php]PHP Manual: User-Defined Functions[/url]

[url=http://devzone.zend.com/node/view/id/637]PHP 101 (part 6): Functionally Yours[/url]

The function's name does not get parenthesized when defining it, the parentheses surround it parameters (a.k.a. arguments), or are left empty if it has no parameters;
[code=php]
function hello()
{
return "Hello, world.";
}

echo "<p>" . hello() . "</p>n";
[/code]
[/QUOTE]


thx NogDog, I feel embarrassed. I should have known.

thx for the links also

ano
Copy linkTweet thisAlerts:
@anothenauthorSep 13.2010 — uh oh, still can't get it to work.

I'm sure I'm missing something simple.

<?php

function testpair_(){

switch($whichpair){

case "EURUSD" : $testpair = "eurusd"; break;

case "USDEUR" : $testpair = "eurusd"; break;

}

} // end function

?>

<?php

$whichpair = "EURUSD";

testpair_();

echo $testpair;

?>
Copy linkTweet thisAlerts:
@ChipzzzSep 13.2010 — You were very, very close... try this:
<i>
</i>&lt;?php

function testpair_($whichpair){

switch($whichpair){
case "EURUSD" : $testpair = "eurusd"; break;
case "USDEUR" : $testpair = "eurusd"; break;
}
return $testpair;
} // end function
?&gt;

&lt;?php
echo testpair_('EURUSD');
?&gt;



Cheers ?
Copy linkTweet thisAlerts:
@anothenauthorSep 13.2010 — YAAY! ** dances around **

Finally.

I couldn't quite grasp "return" and

taking the contents of the function and saving to a variable.

That is:

$testpair = testpair_($whichpair);

Because the name of the function and the name of the variable

was the same, it kept confusing me, and I was wondering

why it would not pass the variable contents.

But I got it working.

Thx Chipzzz




You were very, very close... try this:
<i>
</i>&lt;?php

function testpair_($whichpair){

switch($whichpair){
case "EURUSD" : $testpair = "eurusd"; break;
case "USDEUR" : $testpair = "eurusd"; break;
}
return $testpair;
} // end function
?&gt;

&lt;?php
echo testpair_('EURUSD');
?&gt;



Cheers ?[/QUOTE]
Copy linkTweet thisAlerts:
@ChipzzzSep 13.2010 — Glad I could help you out ?
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...