PHP - There Are In And Out Parameters In Php Functions?
Hi,
I develop some functions in PL/SQL and PL/PgSQL. In these languages are IN and OUT parameters in Functions and Store Procedures. There is IN and OUT parameters in PHP Functions? Sorry my bad english. Best Regards, Similar TutorialsI've been wasting time reinventing the wheel with this one so I'd greatly appreciate if people here could share how they deal with this. So heres an example class:
class VarDataClass { private $module = 'show_vars'; public $category = 'DEFAULT CATEGORY'; public $title = 'DEFAULT TITLE'; public $option2 = 'DEFAULT VALUE'; public $show = 'SOMETHING'; public $output = 'box'; public $_settings = array( 'category' => 'DEFAULT CATEGORY', 'title' => 'DEFAULT TITLE', 'option2' => 'DEFAULT VALUE', 'show' => 'SOMETHING', 'output' => 'box' ); public function __constructor($options = array()) { } // some functions }So the $options parameter in the constructor contains all the custom settings for the class instance, lets say in this case they're all optional. Since they're all optional I made that $_settings array have default values. And for this example, I added another way I could do it, by adding the variables individually rather than as part of the settings array. So I'm wondering whats the best way to replace the default values with any values that the user happens to input. Is a list of variables easier, or is the $_settings array the way to go? I think the settings array would be the way to do it, so I'm thinking I should make some option setter function, but whats the easiest way to do that? Should I loop through every key in the settings array, and check if that key is present in the $options variable that the user input? Or is there some kind of array_replace function I can use that will set all the options in one go without having to use a loop? Another issue is its a pain in the ass for the user to input an associative array, its much easier to just to $object = new VarDataClass('CATEGORY','TITLE');etc. but then the problem is if they only want to set say the two last options, they'd have to do VarDataClass('','','','SHOW','OUTPUT');Basically I'm just looking for the most evolved way to handle these optional functions so I don't have to reinvent the wheel myself. Edited by entheologist, 29 August 2014 - 10:55 AM. I am calling CURL and trying to do a POST request with parameters: Code: [Select] $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Accept: application/json", "Content-Type: application/json")); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_USERAGENT, "curl 7.23.1 (x86_64-unknown-linux-gnu)"); curl_setopt($curl, CURLOPT_USERPWD, "username:password"); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_URL, "https://www.mydomain.com/route"); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "key1=blah1&key2=blah2"); return curl_exec(curl); The problem, inside the request at http://www.mydomain.com/route I am not seeing any POST parameters passed. I.E. Code: [Select] print_r($_POST); Code: [Select] Array ( ) Should have key1=blah1 and key2=blah2. Any ideas? I have some function or method. Is there a better design patter to implement this?
function myFunction($a=null,$b=null,$c=null,$d=null,$e=null,$f=null,$g=null,$h=null) { //Do a bunch of stuff }Maybe the second function? function myNewFunction($data=array()) { $data=array_merge(array('a'=>null,'b'=>null,'c'=>null,'d'=>null,'e'=>null,'f'=>null,'g'=>null,'h'=>null),$array); //Do a bunch of stuff }Please provide decision making factors why you would use one approach over the other. I have a script I am putting together that simulate a cricket game. The only issue is, that there are a huge number of functions because there doesn't seem to be any other way to do this properly. As well as this, there a while() loop and all this seems to be leading to the page reaching a max 30 second timeout when generating the result. My code is attached below, it is quite messy at the moment because i've just be working on it, but I was wondering if anyone has any solutions of how I can speed this up or change to prevent a timeout: <?php // Error reporting error_reporting(E_ALL); // Connect DB mysql_connect("wickettowicket.adminfuel.com", "rockinaway", "preetha6488") or die(mysql_error()); // Select DB mysql_select_db("wickettowicket") or die(mysql_error()); // MySQL queries to find batsmen and bowlers $array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); $array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); // Start table for data $data = '<table width="600px">'; // Create blank scorecard while ($array_bat = mysql_fetch_array($array_batsmen)) { $data .= '<tr><td>'.$array_bat['name'].'</td><td></td><td></td><td>0</td></tr>'; } // Set up arrays for players $current_batsman = $current_bowler = array(); // Reset query mysql_data_seek($array_batsmen,0); $in_one = $in_two = $it = ''; function currentBatsman($id, $name, $ability, $strength, $out, $in, $runs) { global $current_batsman; $current_batsman = array ( 'id' => $id, 'name' => $name, 'ability' => $ability, 'strength' => $strength, 'out' => $out, 'in' => $in, 'runs' => $runs ); echo 'set current'; } // Set up arrays of batsmen while ($array = mysql_fetch_array($array_batsmen)) { if ($it < 3 && $in_one == '') { currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_one = $array['id']; $current = $array['id']; $it++; } else if ($it < 3 && $in_two == '') { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 1, 'runs' => 0 ); $in_two = $array['id']; $it++; } else { $batsmen[$array['id']] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); } } // Bowler Array while ($array = mysql_fetch_array($array_bowlers)) { $bowlers[] = array ( 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'] ); } // Reset both queries mysql_data_seek($array_bowlers,0); mysql_data_seek($array_batsmen,0); function changeBatsman($just_out) { global $array_batsmen, $batsmen; //Update array $batsmen[$just_out] = array ( 'in' => 1, 'out' => 1 ); while ($array = mysql_fetch_array($array_batsmen)) { if ($just_out != $array['id'] && $batsmen[$array['id']]['out'] != 0) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'change batsman'; } function swapBatsman($other_batsman) { global $array_batsmen, $batsman; while ($array = mysql_fetch_array($array_batsmen)) { if ($other_batsman != $array['id'] && $batsman[$array['id']]['out'] != 0 && $batsman[$array['id']]['in'] == 1) currentBatsman($array['id'], $array['name'], $array['ability'], $array['strength'], 0, 1, 0); } // Reset query mysql_data_seek($array_batsmen,0); echo 'swap batsman'; } $runs = $outs = $balls = $overs = 0; $played = array(); function selectBowler() { global $bowlers, $current_bowler; // Select random bowler $choose_bowler = array_rand($bowlers, 1); $current_bowler = array ( 'name' => $bowlers[$choose_bowler]['name'], 'ability' => $bowlers[$choose_bowler]['ability'], 'strength' => $bowlers[$choose_bowler]['strength'] ); } /* function selectBatsman(); { global $array_batsmen; while ($array_batsmen[]['out'] != 1) { }*/ function bowl() { global $batsmen, $bowlers, $current_bowler, $current_batsman, $data, $balls, $outs, $runs; if ($current_batsman['out'] == 0) { echo 'bowling'; // Set the initial number $number = rand(0, 190); // Ability of batsman if ($current_batsman['ability'] > 90) $number += 30; else if ($current_batsman['ability'] > 70) $number += 15; else if ($current_batsman['ability'] > 50) $number += 2; else $number = $number; // Strength of batsman if ($current_batsman['strength'] > 90) $number += 15; else if ($current_batsman['strength'] > 70) $number += 10; else if ($current_batsman['strength'] > 50) $number += 5; else $number = $number; // Depending on overs if ($balls > 270) $number += 30; else if ($balls > 120) $number -= 10; // Ability if ($current_bowler['ability'] > 90) $number -= 30; else if ($current_bowler['ability'] > 70) $number -= 15; else if ($current_bowler['ability'] > 50) $number -= 2; else $number = $number; // If batsman has made a huge total of runs, we need to knock some numbers off - more likely to get out if ($current_batsman['runs'] > 200) $number -= 70; else if ($current_batsman['runs'] > 100) $number -= 30; // Finally sort out runs if ($number > 190) $run = 6; else if ($number > 170) $run = 4; else if ($number > 160) $run = 3; else if ($number > 100) $run = 2; else if ($number > 50) $run = 1; else if ($number > 10) $run = 0; else if ($balls > 120 && $number > 0) $run = 0; else $run = -1; // Increase number of balls $balls += 1; // Are they out? if ($run == -1) { $current_batsman['out'] = 1; $played[] = $current_batsman['id']; $find = '<tr><td>'.$current_batsman['name'].'</td><td></td><td></td><td>0</td></tr>'; $replace = '<tr><td>'.$current_batsman['name'].'</td><td></td><td>'.$current_bowler['name'].'</td><td>'.$current_batsman['runs'].'</td></tr>'; $data = str_replace($find, $replace, $data); changeBatsman($current_batsman['id']); echo 'out'; } else { $current_batsman['runs'] += $run; $runs += $run; if ($run == 1 || $run == 3) { swapBatsman($current_batsman['id']); echo 'time to swap'; } echo $run; } // Count outs if ($current_batsman['out'] == 1) $outs += 1; } } function game() { global $main, $batsmen, $bowlers, $data, $batted, $balls, $outs, $current_batsman; // Check if possible while ($balls <= 295 && $outs < 10) { selectBowler(); // Actually bowl now bowl(); } } game(); echo $data; I teaching myself php, but I am coming from java and other compiled languages, so the process has been a little bumpy. I am trying to do something like this: Code: [Select] class my_class { function one () { $two = two (); $three = three (); $five = $two + $three; return $five; } function two () { $two = 2; return $two; } function three () { $three = 3; return $three; } } Unfortunately, I keep getting an error message saying that my call to two () is an undefined function. I am gathering from this that the scope of one () is not aware of the existence of two (). Is there a way to get around this so I can call two () and three () from one ()? Hi hello, Right now I have this generated URL: mywebsite.com/thisismypage?compare0=50&compare1=112&compare2=512 Is there anyway to "extract" the 50, 112, and 512 dynamically? Those numbers are the Item ID's, I need them for further operations. So is there a way to extract the IDs, maybe into an array? The ID's are dynamically rendered, as well as the "compare#". Not sure if I am being clear... hopefully this makes sense. All help is appreciated. Thank you. I got two links with a parameter with a value. When I click either link it will send me to the same challenge.php page, I'm wanting to find the value of the parameter and display the page according to what value the parameter has in the url. This script doesn't seem to be working. Code: [Select] <a href="http://www.site.com/challenge.php?id=1">Complete Challenge 1</a> <a href="http://www.site.com/challenge.php?id=2">Complete Challenge 2</a> Code: [Select] $id = $_GET['id']; if ($id=1) { echo "This is challenge 1 completed"; } elseif ($id=2) { echo "This is challenge 2 completed"; else { echo "The URL parameters didn't work"; } Any help would be appreciated thanks. Is there an alternative way to encode url params besides: base64_encode() examples would be great. I am making a function it returns one thing, but I would the like to put something into a variable to use outside the function. for example, the third parameter in preg_match() sets a variable that you can then use later in your code, how can I create a parameter like that? Hello, I have made some dynamic code that performs a query on the SQL database, I only know that query at run time. However, after reading some more about PHP, I noticed that I need to include the function mysql_real_escape_string before doing this line: @mysql_query( $aStatement); So, how can I read that $aStatement and then call the mysql_real_escape_string function for each needed parameter? Thanks. Hello all, I'm kind of a noob to PHP, and as such have been teaching myself the language. I'm attempting to insert tag attributes in divs, based on whether the site is viewed in IE or Firefox, to account for subtle differences in appearance. The problem, however, is that the variable $boxorlink apparently has the value "box" the second time I call the method, even though I explicitly state "other" as the parameter; the else clause is never reached. If anybody could tell me why this is happening, please let me know. My entire HTML code is below... <html> <head> <style type ="text/css"> html { background-image:url('denim.jpg'); } .wrapper { min-width:600px; width:600px; height:800px; position:fixed; } .headertext { font-weight:bold; color:#FFFFFF; font-size:24pt; font-family:sans serif; text-shadow: 0.1em 0.1em #333 } .miniheadertext { font-weight:bold; color:#FFFFFF; font-size:16pt; font-family:sans serif; text-shadow: 0.1em 0.1em #333 } div.transbox { width:750px; height:180px; margin:30px 50px; border-style:solid; border-color:#c0c0c0; background-color:#000000; /* for IE */ filter:alpha(opacity=30); /* CSS3 standard */ opacity:0.3; } div.currentpagelink { width:90px; height:22px; margin:30px 50px; background-color:#c0c0c0; font-color:#FFFFFF; font-weight:bold; font-size:14pt; font-family:sans serif; text-align:center; /* for IE */ filter:alpha(opacity=40); /* CSS3 standard */ opacity:0.4; } } div.otherpagelinks { width:100px; height:50px; font-size:14pt; font-family:sans-serif; } </style> <title> Marvin Serrano's Online Portfolio </title> <?php function checkBrowser($boxorlink, $left, $top, $displaceleft, $displacetop) { //new positions for Firefox $newleft = $left + $displaceleft; $newtop = $top + $displacetop; //positions for links... $newlinkleft = $left - $displaceleft; $newlinktop = $top - $displacetop; //checks for box on top or links; links have to be certain % from top. if ($boxorlink = 'box') { if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== FALSE) { echo('left:'.$left.'%'); } else { echo('left:'.$newleft.'%'); } } elseif ($boxorlink = 'other') { if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== FALSE) { echo('left:'.$left.'%;'.'top:'.$top.'%'); } else { echo('left:'.$newlinkleft.'%;'.'top:'.$newlinktop.'%'); } } } ?> </head> <div class="wrapper"> <div class ="transbox" style="position:absolute; <?php checkBrowser('box',19,0,9,0) ?>"> <div class ="headertext" style="position:absolute; left:30%; top:30%">MARVIN SERRANO</div> <div class ="miniheadertext" style="position:absolute; left:29%; top:55%">Live for today, dream for tomorrow.</div> </div> <div class ="currentpagelink" style="position:absolute; <?php checkBrowser('other',38.1,26.3,10.1,3) ?>"> Home </div> <div class="otherpagelinks"> </div> </div> </html> -Marvin
Not experienced coding PHP and need help doing something which is probably very easy but looks like Mt Everest to me. FOO = 123456 BAR = abc Then, write URLs with those variables and have them work the same in a browser:
http://webpage1.com/?var1=FOO&var2=BAR
http://webpage2.com/?var1=FOO&var2=BAR
http://webpage3.com/?var1=FOO&var2=BAR If I update the fields in my local webpage and refresh browser, would like the new value(s) to be used instead. Any help with this greatly appreciated. If someone will put a file together think that I can edit and modify from there for my specific application. Thank you. Edited December 1, 2020 by XcloneHi all, I need to get a parameter from an url, but the GET variables are passed through urldecode(). Some of my variables may have characters such as "+", ":", spaces, etc. When I use GET, all "+" are converted to spaces. I can use urlencode to get the "+" back, but also everything that was previously a space also turns into "+". Example: variable = abc+ c+:d Then I can get either: abc c :d or abc++c+:d but I want to get abc+ c+:d Would anybody know how to help me? Thanks! I'm making a game in php and I'm wanting users to scan a QR code to get a URL, which takes them to a page on my website. I need the URL to have a parameter to identify the QR code and then be able to give the user a reward according to what QR code took them to the website. Anybody have any idea how I would go about this? Thanks. This thread is about what you can and cannot call Variables and Parameters in a Function... I have this query which returns 1 or 2 for $gender... Code: [Select] // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt1)==1){ // Member was Found. // Bind result-set to variables. mysqli_stmt_bind_result($stmt1, $firstName, $username, $photoName, $photoLabel, $gender, $birthYear, $location, $occupation, $interests, $aboutMe); And here is my Function... Code: [Select] function displayGender($gender){ if ($gender == 1){ $gender = 'Male'; }elseif ($gender == 2){ $gender = 'Female'; }else{ $gender = ''; } return $gender; }//End of displayGender Questions: 1.) Is it a problem having $gender as both my Parameter and Argument? 2.) Is it a problem renaming $gender inside the Function like I do? 3.) Is it a problem returning $gender like I do? Thanks, Debbie So I'm creating a website for bar deals... I've tried for hours trying to get the url to take in more than one parameter with no luck, I don't know if my database isn't setup correctly or something but the url would look like this http://www.site.com/?id=1&day=Monday and when those parameters are passed the deals for that id and day are then posted in the content area I'm pretty much stumped. I feel like it's much easier than I am making it. My database looks like: ID Name Monday Tuesday Wednesday -> and so on 1 Bar1 MondayDeal TuesdayDeal WednesdayDeal 2 Bar2 MondayDeal -> and so on Hello All, I have created a web application. Unfortunately at development time I forgot to encrypt my query parameter. So generally my apply URL shown like : http://mysite.com/myprofile.php?userId=2 So here any user can guess some other Ids because User Ids are not encrypted in URL. Is there any simple way to do it. I have more than 100 php pages and implementing ecry/decry logic on each page is time consuming So is there any way around? Regards, Prateek Hi everybody, I wrote a web service and I need to pass parameters through url in order to make a record in a mysql db. The problem is that when I insert greek characters in url it translates it to something like this http://xxxxxx.xx/test3.php?name=%CD%E9%EA%EF%F2&pass=234 and I got an error. Does anybody have a suggestion? Please help because I search for this many hours and still nothing. Thanks for your time Hello again, Though this question is somewhat related to my prior note, I thought it was probably different enough to justify a separate topic... So, I have a link at site1.com/dir/admin/?param1=value¶m2=value, etc, which I want to redirect to site2.com with all the rest being the same. Thanks to your help, it's working with two specified parameters However, there is a relatively short list of potential parameters (maybe 5?) but not all of them are passed in all calls to the link. In other words, sometimes the link is called with param1 and param2, sometimes with 1 and 3, sometimes with 1,2, and 4, etc. What is the best way to do the redirect which would ensure that all the parameters which are sent to the initial link are then included in the redirect? Is there a way to search the incoming link and grab all parameters in it? Or do I put together the list of possible parameters and then grab the values with $_GET for all of them and then pass all of them even if the values are null (not sure whether the app will accept that or not...) I hope I'm stating the question clearly enough! Thanks in advance for your help! Hi, I am somewhat new to PHP but I have a little experience. I am having trouble coding this script to set variables with extra quotations and replacing some exploded strings. Here is the script. Code: [Select] <?php $ToCutDown = "[{"parentMessageId":-1,"message":"%3Ca%20href%3D%27%23%27%20class%3D%27standardLink%27%20onclick%3D%27showMobStats%28674542538%29%3B%27%3Eaka%20Bubbles%3C%2Fa%3E%20broadcast%20a%20message%3A%20%3Cfont%20color%3D%27red%27%3E%22Place%20Bounty%20on%20%26quot%3Baka%20Bubbles%26quot%3B%20%28Minimum%20of%20%2418%2C107%2C899%2C000%29%22%3C%2Ffont%3E%2E","id":28152301,""; $Exploded = explode("[{"parentMessageId":-1,"message":"", $ToCutDown); $Exploded = explode(","id":28152301,"", $Exploded[0]); $Exploded = urldecode($Exploded[1]); $StringReplace = str_replace("<a href='#' class='standardLink' onclick='showMobStats(674542538);'>", "", $Exploded); ?> So I'm trying to work with specific strings that have quotation marks in them (Which cannot be removed) & I'm having a hard time using them in variables and in any function that requires you to choose parameters with either ' ' or " ". Any suggestions would be appreciated thanks MOD EDIT: [code] . . . [/code] tags added. |