Davorin Savovic
Davorin Savovic

Reputation: 61

Variable inside the link

Would someone be able to help me with some php. I am new to this and I am trying to solve the puzzle.

I am trying to combine the input data that user has provided with the link so that final output displays record for the user whose regid was provided by user via input text field.

Here is some code I came up with that obviously does not work.

class Fields_View_Helper_FieldStats extends Fields_View_Helper_FieldAbstract
{
  public function fieldStats($subject, $field, $value)
  {
    $userid = preg_replace(trim($value->value));

    // create user's profile address using their username/userid
    $stats = $userid;

    echo '<div style="margin:0px auto;"><script type="text/javascript" src="http://e1.statsheet.com/embed/';
    return $this->view->string()->chunk($value->value);
    echo '/1/NuNes.js"></script></div>';
  }
}

Upvotes: 0

Views: 58

Answers (2)

Fleshgrinder
Fleshgrinder

Reputation: 16253

The return ends the method, because it returns the value to the caller.

<?php

function fn() {
  return "bar";
}

echo fn(); // will output bar

function fn2() {
  echo "foo";
  return "bar";
}

echo fn2(); // will output foobar

function fn3() {
  return "foo" . fn();
}

echo fn3(); // will output foobar as well

?>

And here's how you can connect those three lines in the code snippet you posted:

<?php
class Fields_View_Helper_FieldStats extends Fields_View_Helper_FieldAbstract
{
  public function fieldStats($subject, $field, $value)
  {
    $userid = preg_replace(trim($value->value));

    // create user's profile address using their username/userid
    $stats = $userid;

    return
      '<div style="margin:0px auto;"><script type="text/javascript" src="http://e1.statsheet.com/embed/' .
        $this->view->string()->chunk($value->value) .
      '/1/NuNes.js"></script></div>'
    ;
  }
}

?>

And here's how you can concatenate strings:

<?php

$string1 = 'foo ' . fn() . ' bar';

$string2 = "foo 2" . fn() . " bar";

?>

And here's how you can embed stuff in variables (faster):

<?php

$string1 = fn();
$string1 = "foo {$string1} bar";

// Or with an object
class Foo {
  public function fn(){}
}
$foo = new Foo();
$string1 = "foo {$foo->fn()} bar";

?>

Upvotes: 1

Machavity
Machavity

Reputation: 31614

To concatenate a string in PHP do this (note the periods, which are doing the work)

$str = "Line 1 " . $somevar . " Line 2";
return $str

Issuing a return terminates your function. I would build one string inside a variable then return that variable

Upvotes: 1

Related Questions