View Full Version : php subs ?!
sir pannels
12-22-2002, 02:27 AM
hey
hmm i dont know if it is subs im talking about here.
but in php say i want to write a script with a few different parts.. lets say 5. completly different parts... and to access each its like script.php?actionpart2 or script.php?action=part2
whatever
is this done with subs?
i can do it in perl.. not with php tho i just imgen it can be done.
any ideas?
cheers :)
P
fractalvibes
12-22-2002, 04:17 AM
I think with PHP you are talking about functions instead; but I could be wrong! Methinks with PHP uses all functions, much like javascript or C.
Something like:
<?php
function AddUser($txtUserName,$txtPassword) {
do some stuff
}
?>
bunch of html here...
<?
If ($btnSubmit > "") {
AddUser($txtUserName,$txtPassword,$txtEmail);
}
?>
Phil J.
Dylan Leblanc
12-22-2002, 04:38 AM
you could do something like:
switch ($_SERVER['QUERY_STRING']) {
case'action1':
...
break;
case'action2':
...
break;
case'action3':
...
break;
default:
...
break;
}
firepages
12-22-2002, 07:29 AM
PHP does not have subs as such just functions as fractalvibes suggests though if you create a function without a return value its technicaly a sub I suppose ?
script.php?action=part2
gives you the variable $_GET['action']
you can switch it as Dylan suggests or fire off a function depending on the value of $_GET['action']; - in PHP you can use a variable as the function name so
<?$_GET['action']();?>
is the same as
<?part2() ;?>
i.e.
<?
function part1(){
echo 'you called function \'part1\'';
}
function part2(){
echo 'you called function \'part2\'';
}
function part3(){
echo 'you called function \'part3\'';
}
//etc//
if($_GET['action']){
//this is the same as calling part2() or part1() etc//
$_GET['action']();
}else{
//do something else//
}
?>
Another popular way is to include a file depending on the URL, this is especially useful if the contents of your different function calls are long-winded.
<?
@include('./includes/'.$_GET['action'].'.php');
?>
eg that would include ./includes/part2.php
Dylan Leblanc
12-22-2002, 07:36 AM
Just be sure to validate your incoming data!
yup
action=phpinfo
for example might tell your visitors a little more than you wanted them to know :)
sir pannels
12-24-2002, 02:22 PM
Heym
sorry i took so long getting back here busy busy busy.
Thanks aload everyone.. really helpfull.. im gona play about with some of that now.. the "case" is basically the same as in C and i know C so i should hopefully not have probs
cheers :thumbsup:
P
sir pannels
12-24-2002, 02:27 PM
ok umm ive got an error. lol i spoke to soon..
this is the code at he mo
<?
function reply(){
$form ="<p align='center'><small><font face=\"Verdana\">Replying To Message:";
$form.="<form action=\"sendmessage.php?id=$id&to=$from\"";
$form.=" method=\"post\">Type your reply:<br> ";
$form.="<textarea rows=\"15\" name=\"message\" cols=\"61\"></textarea><br>";
$form.="<input type=\"submit\" size=\"7\" value=\"Send Reply!\">";
$form.="</form></p></font<</small>";
echo($form);
}
function new(){
echo("<p align=\"center\"><small><font face=\"Verdana\">Composing a new message:");
$form.="<form action=\"sendmessage.php?id=$id&to=$to\"";
$form.=" method=\"post\">Send message to: ";
$form.="<input type=\"text\" size=\"25\" name=\"to\"><br>Commander Name ";
$form.="<input type=\"radio\" name=\"type\" value=\"commander\" checked> Terra Firma Name ";
$form.="<input type=\"radio\" name=\"type\" value=\"base\"><br>";
$form.="<textarea rows=\"20\" name=\"message\" cols=\"70\"></textarea><br>";
$form.="<input type=\"submit\" size=\"7\" value=\"Send Message!\">";
$form.="</form></p></font<</small>";
echo($form);
}
if($_GET['action']){
//this is the same as calling part2() or part1() etc//
$_GET['action']();
}else{
echo("no action!");
}
?>
but i get this error:
Parse error: parse error, expecting `T_STRING' in c:\phpdev\www\public\game\comms\compose.php on line 12
line 12 is the one above where the second function starts.
any ideas?
cheers
:)
raptori
12-24-2002, 02:50 PM
try this
function reply(){
echo "<p align=\"center\"><small><font face=\"Verdana\">Replying To Message:"
."<form action=\"sendmessage.php?id=$id&to=$from\""
." method=\"post\">Type your reply:<br> "
."<textarea rows=\"15\" name=\"message\" cols=\"61\"></textarea><br>"
."<input type=\"submit\" size=\"7\" value=\"Send Reply!\">"
."</form></p></font<</small>";
}
function neww(){
echo "<p align=\"center\"><small><font face=\"Verdana\">Composing a new message:"
."<form action=\"sendmessage.php?id=$id&to=$to\""
." method=\"post\">Send message to: "
."<input type=\"text\" size=\"25\" name=\"to\"><br>Commander Name "
."<input type=\"radio\" name=\"type\" value=\"commander\" checked> Terra Firma Name "
."<input type=\"radio\" name=\"type\" value=\"base\"><br>"
."<textarea rows=\"20\" name=\"message\" cols=\"70\"></textarea><br>"
."<input type=\"submit\" size=\"7\" value=\"Send Message!\">"
."</form></p></font<</small>";
}
if($_GET['action']){
//this is the same as calling part2() or part1() etc//
$_GET['action']();
}else{
echo("no action!");
}
?>
new is some sort of a build function in php:rolleyes:
sir pannels
12-24-2002, 03:03 PM
hehe so simple. cheers that worked.
one more thing tho.. the $_GET var.. is that newish? as in php4 =>
cuz it just says no action whaveter i send from the query string
cheer s:)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.