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?

Hassaan
  • 6,770
  • 5
  • 29
  • 47
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
  • 477
  • 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;
    }
Vishal Solanki
  • 2,415
  • 3
  • 20
  • 39
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,708
  • 8
  • 11