General Community > Scripting Help

cgi 2 php help

<< < (2/2)

Chris Cromer:
Yeah your right, I remember Parham telling me that all variables are global everywhere... unlike in php where you only have super globals and you have to globalize the ones you want.

Parham:

--- Quote from: Chris Cromer on August 10, 2003, 06:13:37 AM ---Yeah your right, I remember Parham telling me that all variables are global everywhere... unlike in php where you only have super globals and you have to globalize the ones you want.

--- End quote ---

Yeah that's one of the downfalls of perl I think; the fact that a variable automatically becomes global no matter what scope you declare it in.  Then there's the fact that "use strict;" with "my/local" is not FORCED.

Aquilo:
but converting Perl to Php it would be:

function test($x) {
     global $y;
}

right?

here is a sample sub

--- Code: ---sub no_card() {
    my($x) =@_; my($y);
    print "<table width=600 border=0 align=center>\n";
    print "\t<tr><td width=100>\n";
    print "\t<img src=\"$img_url/back.gif\"><br>\n";
    print "\t<b>$x -";
    print $meanings[($x -1)];
    print "</b>\n";
    print "\t</td>\n\t<td align=center>";
    print "\t<form action=\"$script_url\" method=POST>";
    for ($y=1;$y <= 10; $y++){
       if ($params{"$y"}){
           print "\t<input type=hidden name=\"$y\" value=\"$params{$y}\">\n";
       }
    }
    print "No card chosen for this position<br>";
    print "<input type=\"functionmit\" name=\"Choose Card\" value=\"Choose Card\">";
    print "</form></td></tr>";
    print "<tr><td colspan=2><hr></td></tr></table>";

}

--- End code ---

and the sub as php function

--- Code: ---function no_card($x) {
    global $y;
}

--- End code ---

since every thing is global in perl the why declare it for the sub?
at first when I looked at the sub I was woundering where it was
getting $params{"$y"} from, so why my($x) =@_; my($y);??

also while asking what is this?

--- Code: ---use Time::Local qw(timegm);

use vars qw ($one $two $three $more $varibles);
$one = 1.111;
$two = 2.222;
$three = 3.333;
# ect...

--- End code ---

I think the time bit can be removed and just use the Date(), Time(), MkTime()
functions in php when called for but what is qw() doing and then the
variables=values bit??

Thanks for the help in understanding this, there was so much I didn't know
2 years ago and now I know less! ;D

[Unknown]:

--- Code: ---function no_card($x)
{
    echo '<table width="600" border="0" align="center">
   <tr>
   <td width="100">
      <img src="', $img_url, '/back.gif" alt=""><br />
      <b>', $x, ' - ', $meanings[$x -1], '</b>
   </td>
   <td align="center">
      <form action="', $script_url, '" method="post">';

    for ($y = 1; $y <= 10; $y++)
   {
       if ($_REQUEST[$y])
       {
          echo '
         <input type="hidden" name="', htmlspecialchars($y), '" value="', htmlspecialcahrs($_REQUEST[$y]), '" />';
       }
    }
    echo '
         No card chosen for this position<br />
         <input type="functionmit" name="Choose Card" value="Choose Card" />
      </form>
   </td>
   </tr>
   <tr>
   <td colspan="2"><hr /></td>
   </tr>
</table>';
}
--- End code ---

No globals...

The time stuff... not sure, probably can.

-[Unknown]

Parham:
I think your misunderstanding the definition of my().  my() means to declare the variable inside the current scope only (a scope being the closest braces to the variable).

In this subroutine, your stating that $y will be used in this scope.  You declare it so that "use strict;" won't dish out warnings.  $y isn't coming from anywhere, it's being set and used in this scope.

--- Code: ---sub no_card() {
    my($x) =@_; my($y);
    print "<table width=600 border=0 align=center>\n";
    print "\t<tr><td width=100>\n";
    print "\t<img src=\"$img_url/back.gif\"><br>\n";
    print "\t<b>$x -";
    print $meanings[($x -1)];
    print "</b>\n";
    print "\t</td>\n\t<td align=center>";
    print "\t<form action=\"$script_url\" method=POST>";
    for ($y=1;$y <= 10; $y++){
       if ($params{"$y"}){
           print "\t<input type=hidden name=\"$y\" value=\"$params{$y}\">\n";
       }
    }
    print "No card chosen for this position<br>";
    print "<input type=\"functionmit\" name=\"Choose Card\" value=\"Choose Card\">";
    print "</form></td></tr>";
    print "<tr><td colspan=2><hr></td></tr></table>";

}

--- End code ---

this code on the other hand say the following:  a variable $x is being passed into the function.  There is also a variable $y which was declared outside the function, but you want to use it inside.

--- Code: ---function no_card($x) {
    global $y;
}

--- End code ---

here is an example:


--- Code: ---$y = 'something';
$x = 'something else';
function myfunction($x) { //variable $x being passed in
global $y; //variable $y being globalized to use inside this function
}

--- End code ---

the equivalent of my() in PHP is simply setting the variable inside the function:


--- Code: ---$y = 'something';
function myfunction($x) { //variable $x being passed in
$y = 'something else'; //variable $y has been declared and will only exist inside this scope (function)... this is the equivalent of "my $y;"
}

--- End code ---

as for your third piece of code:


--- Code: ---use Time::Local qw(timegm);

use vars qw ($one $two $three $more $varibles);
$one = 1.111;
$two = 2.222;
$three = 3.333;
# ect...

--- End code ---

the first line can easily be substituted with one of php's functions.  This line, "use vars qw ($one $two $three $more $varibles);", is the money line though.

With "use strict;", you'd think that you would have no way to set global variables.  The above code does exactly that, and it bypasses "use strict;" by packaging the variables something something something zZzZzZzZzZ (boring explanation).  So that line just gives you a reasonable way to set global variables without getting warnings.  An equivalent to that would, I guess, be our(), although I never ended up using it.  I think perl6 properly implements our() so you won't have to resort to the funky code above.

Navigation

[0] Message Index

[*] Previous page

Go to full version