You are here: Re: anything incorrect? « PHP Programming Language « IT news, forums, messages
Re: anything incorrect?

Posted by farrishj@gmail.com on 05/28/07 18:33

On May 28, 1:05 pm, vinnie <centro.ga...@gmail.com> wrote:
> why i do not see anything but a blank page?
>
> <?php
> function somma($uno,$due)
> {
> $totale = $uno + $due;
> return($totale);
> }
> $sum = somma(3,4);
> $totale = $resto;
> print("$resto");
> print("$totale");
> ?>

First things first: use a code formatting style that is easier to
read. Visit http://en.wikipedia.org/wiki/Prettyprint to learn more.

<code>
// I like C-style
<h4>Test of somma function</h4>
<p>
<?php

function somma($uno,$due) {
$totale = $uno + $due;
return $totale;
}

$sum = somma(3,4);
$totale = $sum; // Here, this was $resto, which is not pre-assigned a
value
print($resto); // Before, printed nothing, since $resto did not point
to anything
print($totale); // Before, a pointer to nothing ($resto)

?>
</p>
</code>

The error may be a scoping issue, an error in naming a variable, or
both. However, php is doing what is expected.

Since $totale is in a function where it is assigned a value, it is
considered "in the scope of the function" and is not available to the
global-space variable declaration. To make it accessible, you have to
import the global $variable:

<code>
function somma($uno,$due) {
global $totale;
// continue code with $totale available
}
</code>

You can also pass by reference:

<code>
function somma($uno, $due, &$totale) { // <-- Notice ampersand (&)
$totale = $uno + $due;
return($totale);
}
</code>

But then, somewhat inexplicably, you assign $totale to a non-declared
$sum variable (which will make $totale === null), and then try to
print both.

Note how I put a page title and a horizontal rule (<hr />) tag before
and after the code I expect to output for testing. This tells me if
the page died on parse (one kind of error, like not escaping a
sequence correctly), if the page died on processing (like calling a
function that doesn't exist), or if, in this case, the code has
nothing to output. This helps troubleshoot the code more efficiently
and effectively.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация