April 16, 2012

php function arguments

Question by Clint Chaney

I don’t know how or where I got this idea in my head but for some reason I thought this was possible. Obviously after testing it doesn’t work, but is there a way to make it work? I want to set $value2 without having to enter anything at all for $value1.

function test($value1 = 1, $value2 = 2) {

echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';

}

test($value2 = 3);

// output
Value 1: 3
Value 2: 2

Answer by rami

What you’re trying to do is called “keyword arguments” or “named arguments” and is not available in PHP in contrast to other scripting languages like Python.

If you have functions with hundreds of parameters and really want to achieve a more flexible solution than what PHP comes with, you could build your own workaround with arrays or objects, maybe in conjunction with func_get_args(). But this obviously isn’t as beautiful as real keyword arguments.

Answer by Starx

Its not entirely possible the way you want.

Simply,

function test($value1 = null, $value2 = 2) {

echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';

}

test(NULL, $value2 = 3);

Or, Use array as parameters

function test($array) {

if(isset($array['value1'])) echo 'Value 1: '.$array['value1'].'<br />';
if(isset($array['value2'])) echo 'Value 2: '.$array['value2'].'<br />';

}

test(array('value2' => 3));

Update:

My another attempt

function test() {
  $args = func_get_args();
  $count = count($args);
  if($count==1) { test1Arg($args[0]); }
  elseif($count == 2) { test2Arg($args[0],$args[1]); }
  else { //void; }
}

function test1Arg($arg1) {
   //Do something for only one argument
}
function test2Arg($arg1,$arg2) {
   //Do something for two args
}
April 5, 2012

missing ) after argument list on line 1

Question by Rafael

 <script type="text/javascript">$('#multipleResults').show(); $('#multipleResultsOut').html('<table><tr><td colspan="2"><?php echo $lang['search.limited']; ?></td></tr><?php while ($bQr = mysql_fetch_assoc($botquery22)) { echo ('<tr><td style="width: 40px;"><div style="width: 40px; height: 40px; overflow: hidden; background-position: center; background-repeat: no-repeat; background-image: url('%www%/images/badges/'.$bQr['name'].'');"></div></td><td><a href="#" onclick="getBadge('%www%/', ''.$bQr['name'].''); return false;">'.$bQr['badgename'].'</a><br />'.$bQr['desc'].'</td></tr>'); } ?><?php echo $end; ?>;</script><?php echo $lang['search.x.results']; ?>

And I’ve rolled the code again, but still the same error.

Answer by Starx

May be the problem of quotes, the php snippets are also using single quotes as your js script.

Try them this way

$('#multipleResults').show();
$('#multipleResultsOut').html('<table><tr><td colspan="2">'+
<?php echo $lang['search.limited ']; ?>+'</td></tr>'+
<?php while ($bQr = mysql_fetch_assoc($botquery22)) { 
    echo (' < tr > < td style = "width: 40px;" > < div style = "width: 40px; height: 40px; overflow: hidden; background-position: center; background-repeat: no-repeat; background-image: url('%www%/images/badges/'.$bQr['name'].'');" > < /div></td > < td > < a href = "#" onclick = "getBadge('%www%/', ''.$bQr['name'].''); return false;" > '.$bQr['
badgename '].' < /a><br / > '.$bQr['
desc '].' < /td></tr > '); 
} 
?>
<?php echo $end; ?>;
March 19, 2012

how to pass array string in javascript function from php end as a argument

Question by Samad

Hey Guys i have a little issue of JavaScript and i don’t know how to solve this Query that why i put this Question in stackoverflow also helps are definitely appreciated

I am getting the error missing ) after argument list in my Firebug console.

emissing ) after argument

My Question is how to pass $char_data variable in JavaScript function as a argument

Define php variable

<?php 
$chart_data = "['NBA',1],['NFL',2],['MLB',3],['NHL',4]"; 
$div = "graph";
?

Call Javascript function with define argument

<script>
dynamicChartArray('<?php echo $div;?>','<?php echo $chartdata;?>')
</script>

A function of javascrpit

<script>
function dynamicChartArray(div,chartdata){
var myData = new Array(chartdata);
var myChart = new JSChart(div, 'pie');
alert(chartdata+div);
}
<script>

Answer by Carl

Rather than creating an array out of a string in javascript, why not just just get the PHP to output it as an array to start with?

Just add an extra set of [] which javascript reads as an array.

$chart_data = "[['NBA',1],['NFL',2],['MLB',3],['NHL',4]]"; 

then ditch the quotes on the output (which are responsible for causing the error messages)

 dynamicChartArray('<?php echo $div;?>', <?php echo $chartdata;?>);

and then myData can just equal chart data (since its already an array)

var myData = chartdata;

Answer by Starx

You dont need var myData = new Array(chartdata);.

chartdata is already an array.

...

Please fill the form - I will response as fast as I can!