News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

cgi 2 php help

Started by Aquilo, August 10, 2003, 02:46:19 AM

Previous topic - Next topic

Aquilo

sorry for bothering anyone but what would this be in php
my($x) =@_;
my($y);

I'm thinking $x is a global array or something and "global $y;"
or something  :D

anyway thanks!

Chris Cromer

#1
I beleive that would be like this:

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

But don't hold me to that being correct... I havn't used perl enough to know for sure if that is 100% correct.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Aquilo

I think your right.
I used to know some cgi when I used YaBB.pl but that's been to long
and now I'm gonna need a reference book! ;D

Chris Cromer

The only reason I know any perl is because I converted my PHP scripts to perl in an attempt to learn perl... I also converted perl YaBB mods to php YaBBSE for people.

So my knowlege is limited to what Parham tought me... and what was in YaBB and it's mods. :-\
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

[Unknown]

No, it's like this:

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

This is because ALL variables are global, except my.

-[Unknown]

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.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

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.

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

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>";

}


and the sub as php function

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


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?

use Time::Local qw(timegm);

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


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]

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>';
}


No globals...

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

-[Unknown]

Parham

#9
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.

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>";

}


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.

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


here is an example:


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


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


$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;"
}


as for your third piece of code:


use Time::Local qw(timegm);

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


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.

Advertisement: