I want to use a function to echo a value I named, "name". The value is sent to the function via the POST method from a form.
My HTML:
<html>
<head>
<title> function play </title>
</head>
<body>
<form method="post" action="function.php">
Name: <input type="text" name="name"/>
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
My PHP:
<?php
$name= $_POST['name'];
function writeName($name)
{
echo $name;
}
writeName();
?>
For some reason I keep getting the error:
Warning: Missing argument 1 for writeName(), called in C:\xampp\htdocs\function.php on line 7 and defined in C:\xampp\htdocs\function.php on line 3
Notice: Undefined variable: name in C:\xampp\htdocs\function.php on line 5
What am I doing wrong?
You need to pass a parameter in to the function.
writeName($name);
As others have said, you need to pass your value as as an argument when you call the function.
Variables in PHP are "scoped". This means that wherever you define your variable will affect its context. In your example, $name defined at the script level is not the same as $name defined as a function parameter. However, $name used within your function will always refer to the same variable within that function.
To put that in practice, consider the following:
$name = 'a';
function writeName( $name )
{
echo $name; // echoes 'b'
}
writeName( 'b' );
echo $name; //echoes 'a'
Another way to think about it is by just changing the names of the variables so they are noticeably different:
$getName= $_POST[ 'name' ];
function writeName( $nameToWrite )
{
echo $nameToWrite;
}
writeName( $getName );
You can also use the global keyword inside your function to let PHP know that you're referencing a variable in the global scope (i.e., at the script level, outside your function) but this is generally bad practice.
$getName = $_POST[ 'name' ];
function writeName() // Notice that we don't need to pass $getName as an argument
{
global $getName;
echo $getName;
}
writeName();
You defined your function as function writeName($name), which means it's expecting an argument. But when you call the function, you don't pass an argument. Try changing
writeName();
to:
writeName($name);
You have to pass a parameter to the function writeName($name);
or you can set a default for the function parameter:
function writeName($name = null)
{
echo $name;
}
You're not passing the parameter to the function. Try this:
<?php
function writeName($name)
{
echo $name;
}
writeName($_POST['name']);
?>
The reason is: you declared your writeName function as expecting an argument called $name. But when you call the function, you are not passing any parameters. You should use the function like this:
writeName("some string here");
You've defined your 'writeName' function to accept one parameter. When calling the function you must provide this parameter.
writeName($name);
or
writeName($_POST['name']);
Related
I got strange problem, when i doing echo $setting->samp->url; it works perfectly but when i put it inside a function. It show me Notice undefined variable, what happened here??
$setting = simplexml_load_file('setting.xml');
#Content inside <samp><url>value</url></samp> printed.
echo $setting->samp->url;
sampserver_update();
#Problem
function sampserver_update() {
echo $setting->samp->url;
}
That's because $setting is not available inside the function. You might want to pass it as an argument.
Read about variable scopes here:
http://php.net/manual/en/language.variables.scope.php
Value must be passed as argument to function
sampserver_update($setting);
function sampserver_update($setting) {
echo $setting->samp->url;
}
just replace this lines
Update your function to following
function sampserver_update()
{
$setting = simplexml_load_file('setting.xml');
return $setting->samp->url;
}
Calling and print
echo sampserver_update();
How would I alter the function below to produce a new variable for use outside of the function?
PHP Function
function sizeShown ($size)
{
// *** Continental Adult Sizes ***
if (strpos($size, 'continental-')!== false)
{
$size = preg_replace("/\D+/", '', $size);
$searchsize = 'quantity_c_size_' . $size;
}
return $searchsize;
Example
<?php
sizeShown($size);
$searchsize;
?>
This currently produces a null value and Notice: undefined variable.
So the function takes one argument, a variable containing a string relating to size. It checks the variable for the string 'continental-', if found it trims the string of everything except the numbers. A new variable $searchsize is created which appends 'quantity_c_size_' to the value stored in $size.
So the result would be like so ... quantity_c_size_45
I want to be able to call $searchsize outside of the function within the same script.
Can anybody provide a solution?
Thanks.
Try using the global keyword, like so:
function test () {
global $test_var;
$test_var = 'Hello World!';
}
test();
echo $test_var;
However, this is usually not a good coding practice. So I would suggest the following:
function test () {
return 'Hello World!';
}
$test_var = test();
echo $test_var;
In the function 'sizeShown' you are just returning the function. You forgot to echo the function when you call your function.
echo sizeShown($size);
echo $searchsize;
?>
But the way you call $searchsize is not possible.
This is an old question, and I might not be understanding the OP's question properly, but why couldn't you just do this:
<?php
$searchsize = sizeShown($size);
?>
You're already returning $searchsize from the sizeShown method. So if you simply assign the result of the function to the $sizeShown variable, you should have what you want.
In codeigniter what is the correct method of passing variables to the callback function?
I have used this,
$var1 = 'some conditions';
$this->form_validation->set_rules("callback__is_value_unique[value, $var1]");
public function _is_value_unique($value, $var1){
echo $var1;
die;
}
This gave me output like shown below:-
value, some conditions
rather than,
some conditions
You must only set your value!
$var1 = 'some conditions';
$this->form_validation->set_rules("callback__is_value_unique[value, $var1]");
public function _is_value_unique($value, $var1){
echo $var1;
die;
}
From the docs..
If you need to receive an extra parameter in your callback function,
just add it normally after the function name between square brackets,
as in: "callback_foo[bar]", then it will be passed as the second
argument of your callback function.
Sounds like you could only pass one extra argument. Which should be a string. If you want to pass more arguments, you could store them somewhere else and just pass an argument, which stores the location of the additional param.
$index = count($this->arguments);
$this->arguments[$index] = array('value', 'some conditions'/*, ...*/);
$this->form_validation->set_rules("callback__is_value_unique[$index]");
public function _is_value_unique($value, $index){
$args = $this->argumts[$index];
echo $args[1];
die;
}
I created a php function and I want to clear/reset the arguments of the function.
for example I've got this function declared twice in my index.php:
grid_init($type='portfolio',$postNb=4,$rowNb=2);
grid_init($type='post',$postNb,$rowNb);
function grid_init($type,$postNb,$rowNb) {
?>
<div class="container" data-type="<?php echo $type; ?>" data-postNb="<?php echo $rowNb; ?>" data-rowNb="<?php echo $rowNb; ?>">
some stuff.....
</div>
<?php
}
If I didn't specified my argument in my second function (in the above example $postNb $rowNb), these vars will take the values of the previous argument declared in the previous function ($postNb=4,$rowNb=2)...
How can I reset/clear my argument in my function between each function declared in a same file?
To make a function have default arguments it's like:
function grid_init($type, $postNb = 2, $rowNb = 4){
echo "<div class='container' data-type='$type' data-postNb='$rowNb' data-rowNb='$rowNb'>".
"some stuff.....".
'</div>';
}
Execute like:
grid_init('whatever'); // will assume $postNb = 2 and $rowNb = 4;
grid_init('some_data_type', 42, 11); // overwrite your defaults
You seem to have trouble calling functions.
Change your calls to
grid_init('portfolio',4,2);
grid_init('post','',''); // or use '' as default
a) you might have declared a function like this
function grid_init($type, $postNb, $rowNb)
{
// do stuff on $tyoe, $postNb, $rowNb
}
b) you might call the function several times, each time with new parameters
grid_init('post', 5, 4);
grid_init('somewhere', 1, 2);
A function does not memorize values of prior calls.
If you want that, then save them somewhere from within that function.
c) you might use default parameters on your function
Default parameters always come last in the function declaration.
function grid_init($type, $postNb = 2, $rowNb = 2)
{
// do stuff on $tyoe, $postNb, $rowNb
}
call it
grid_init('somewhere');
now postNb, rowNb are not set, but the default values from the declaration are used.
d) keep the number of parameters low!
I have a function variable like this...
$aName = "My Name";
$sayHelloFunction = public function sayHello($aName){
echo($aName);
}
and I have something like this.....
callAFunctionFromFunction($sayHelloFunction);
Inside the "callAFunctionFromFunction", I do this:
if(is_callable($sayHelloFunction)) {
$sayHelloFunction();
}
I found that the "My Name" can't display, what did I do wrong...
I suggest you to look here and here as these exact threads deal with passing a function as a parameter. Also closures (anonymous functions) in PHP have no name (that's why they are called anonymous), so what you should do is something like that.
<?php $sayHelloFunction = function($aName){
echo($aName);
};
if(is_callable($sayHelloFunction)) $sayHelloFunction("Testing 1,2,3");
The function sayHello expects a parameter $aName, but when you call it you don't pass in a value.
You would need to do this:
if(is_callable($sayHelloFunction)) {
$sayHelloFunction("Hello John");
}
Also you can't use the public access type with closures.
$sayHelloFunction = function sayHello($aName) {
echo($aName);
}