Var_dump with echo output - php

When using var_dump with a function that have an echo like :
<?php
function foo()
{
echo 'Hello';
}
var_dump (foo());
?>
the output is:
HelloNULL
I want to know where the NULL came from

You must set the return the value of the function.
function foo()
{
return 'Hello';
}
var_dump (foo());
Then if you want to retrieve the value of the function just do:
echo foo();

Simply you can return the value in the function foo(). Or just use print_r to print the foo() value.
<?php
function foo()
{
echo 'Hello';
}
print_r (foo());
?>
The output will be Hello.

var_dump always shows the variable type like int or string or etc
When you call the function foo() and has no return type then print Hello and var_dump declares foo() is NULL since it has no return type.
<?php
function foo(){
echo 'Hello';
//return 'StackOverFlow';
}
var_dump(foo());
?>
Look this second one
<?php
function foo2(){
}
var_dump(foo2());
?>
output=>NULL
That means var_dump can't declare what type of variable function foo2()

Related

Create Instance variable in Laravel

I have a php class which having 4 methods.All the 4 methods are using some common variables, I've created those variables as instance variable. But i'm getting an error like "Undefined variable: " How can i solve this problem.
My code is,
public class test{
public static $variable;
public function func(){
$variable = "Hello World";
print_r($variable);
}
}
Actually this code will not gives you any error as you just print the variable which you have defined exactly above print statement.
Demo : http://sandbox.onlinephpfunctions.com/code/cd43e866591ee0693cdcbeec6a230f583f756a67
If you want to assign value to $variable which is defined above the function then try this code :
<?php
class test {
public static $variable;
public function func(){
self::$variable = "Hello World";
print_r(self::$variable);
}
}
$n = new test();
echo $n->func();
?>
Demo : http://sandbox.onlinephpfunctions.com/code/f11b726a678b9a5ee0e474d7ca194bb5ed75af22
If you have same static value for that variable try this
class test
{
const variable ="Hello World";
public function func()
{
echo self::variable;
}
}
$name = new test;
$name->func();

Assigning a function to the variable without calling this function

I want to assigning a function to the variable without calling this function when assigning it to variable.
function abc(){
echo 'abc';
}
and in PHP
$variable=abc();
and then echo variable with function
echo $variable;
EDIT*
Because I want to have variable like this
$variable = $something[0].$function.$something[1];
What you want to use is called Variable functions in PHP and is done like this.
function foo() {
return "Hello I am foo()\n";
}
$func = 'foo';
echo $func(); // This calls foo()
See the PHP Manual page for Variable Functions
Try this:
$variable = function() {
echo 'abc';
};
And then call it like:
$variable();
demo
do like this
$variable=function(){
echo 'abc';
};
$variable();
or
function abc(){
echo 'abc';
}
$variable="abc";
$variable();
Seems like you want to capture what is being echoed (are you using Wordpress?):
ob_start();
abc();
$variable = ob_get_clean();
echo $variable;
Starts a buffer to hold the output and then gets the output from the buffer.

PHP Method Check if used for assignment

I am not sure if this is possible, however, can we build a method that returns or echo the result based on calling the method directly or used in assignment
function foo()
{
return "bar";
}
$abc = foo();
// $abc will have the value "bar"
But if foo() is called directly, it should echo "bar"
foo();
// should echo / print "bar"
adding echo before foo() solves the problem, but how could this be achieved without using echo. Probably adding some line of code to the function foo()
There is a possible way is to echo inside your function .
Just like
<?php function foo()
{
echo "bar";
return "bar";
}
echo $abc = foo();
?>

Make $_GET variable available within function scope

How to pass a $_GET variable into function?
$_GET['TEST']='some word';
public function example() {
//pass $_GET['TEST'] into here
}
When I try to access $_GET['TEST'] in my function, it is empty.
The $_GET array is one of PHPs superglobals so you can use it as-is within the function:
public function example() {
print $_GET['TEST'];
}
In general, you pass a variable (argument) like so:
public function example($arg1) {
print $arg1;
}
example($myNonGlobalVar);
If this is a function and not an object method then you pass the parameter like so
function example($test) {
echo $test;
}
and then you call that function like so
$_GET['test'] = 'test';
example($_GET['test']);
output being
test
However if this is an object you could do this
class Test {
public function example($test) {
echo $test;
}
}
and you would then call it like so
$_GET['test'] = 'test';
$testObj = new Test;
$testObj->example($_GET['test']);
and the output should be
test
I hope this helps you out.
First of all - you should not set anything to superglobals ($_GET, $_POST, etc).
So we convert it to:
$test = 'some word';
And if you want to pass it to the function just do something like:
function example($value) {
echo $value;
}
And call this function with:
example($test);
function example ($value) {
$value; // available here
}
example($_GET['TEST']);
function example($parameter)
{
do something with $parameter;
}
$variable = 'some word';
example($variable);
Simply declare the value for the variable by
declare the function by
function employee($name,$email) {
// function statements
}
$name = $_GET["name"];
$email = $_GET["email"];
calling the function by
employee($name,$email);

Returning a variable from a function in php (return not working)

I'm building an XML page inside of a function, and for some strange reason I don't get the whole thing spit out of the function. I've tried
return $thisXml;
}
echo $thisXML;
and I only get the xml declaration which is in the variable before the function.
If i put an echo in the function, i get everything back as I should.
my page essentially looks like this
$thisXml = 'xml declaration stuff';
function getThisXML($thisXML){
for(i=1; i<5; i++){
$query "has the 5 in it";
while ($mysqlQuery =mysql_fetch_array($theQuery) {
$thisXml.='add the xml';
}
$thisXml.='close the last element';
return $thisXml;
}
echo $thisXml;
as i said, if I replace the 'return' with 'echo', I get all the nice xml. if I echo outside the function, I only get the original declaration.
really strange, and i've been struggling with this one all day.
return $thisXml;
}
echo $thisXML;
$thisXML; only exists in the scope of the function.
Either make $thisXML; global (bad idea) or echo getThisXML() where getThisXML is the function that returns $thisXML;
Are you actually calling the function in the sense of:
$thisXml = getThisXML($someinput);
Maybe a silly question, but I don´t see it in your description.
You need to invoke the function!
$thisXml = 'xml declaration stuff';
echo getThisXML($thisXML);
Or pass the variable by reference:
$thisXml = 'xml declaration stuff';
function getThisXML(&$thisXML){
...
return $thisXml;
}
getThisXML($thisXML);
echo $thisXml;
You have to call the function and apply echo on the returned value:
$thisXml = '…';
echo getThisXML($thisXml);
Or you pass the variably by reference.
You are trying to use a variable defined inside the function scope.
Use:
$thisXML;
function do(){
global $thisXML;
$thisXML = "foobar";
}
print $thisXML;
Returning a variable doesn't mean that it affects that variable globally, it means the function call evaluates to that value where it's used.
$my_var = 5;
function my_func() {
$my_var = 10;
return $my_var;
}
print my_func();
print "\n";
print $my_var;
This will print
10
5
You can create function in php this way:
<?php
$name = array("ghp", "hamid", "amin", "Linux");
function find()
{
$find = 0;
if(in_array('hamid', $name))
{
$find = 1;
return $find;
}
else
{
return $find;
}
}
//###################
$answare = find();
echo $answare;
?>

Categories