0

I wrote a function that I thought cannot be more basic:

In functions.php:

function testowa() {
$stringToReturn = "pies";
return $stringToReturn;
}

Then I'm trying to call it in single.php:

include_once ('functions.php');

testowa();
var_dump($stringToReturn);

And var_dump displays NULL.

Where could I possibly do anything wrong?

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Michał Skrzypek
  • 699
  • 4
  • 14

3 Answers3

3

You have to assign the function's response to a variable. Try

$stringToReturn = testowa();
var_dump($stringToReturn);
Stormhammer
  • 497
  • 4
  • 12
  • Seems I might be too dumb to understand tutorials. Works like a charm, thank you kind sir. I will mark your answer as correct as soon as the system allows me to. – Michał Skrzypek Apr 05 '17 at 13:35
0

@Michał Skrzypek update your function.php file like below:

<?php
    function testowa() {
        global  $stringToReturn;
        $stringToReturn = "pies";
        return $stringToReturn;
    }
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
0

Some ways to rome.

Return Value Version:

function testowa() {
  return = "pies";
}
print testowa();

Reference Version

function testowa(&$refer) {
  $refer = "pies";
}
$refer = '';
testowa($refer);
print $refer;

Global Version

function testowa() {
  global $global;
  $global = "pies";
}
$global='';
testowa();
print $global;

But take the Return Value Version and avoid Global Version

JustOnUnderMillions
  • 3,741
  • 9
  • 12