${'varname_'.$x} variable in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone please remind me how you call this type of variable
${'varname_'.$x}
Thanks!

It's called variable variables!

This should do it:
$varname = 'varname_'.$x;
echo $$varname;

variable varibales!!!
$var = "varname$x";
for($x=0;$x<10;$x++)
{
echo $$var;
}
edit ====
SHOULD BE:
for($x=0;$x<10;$x++)
{
$var = "varname$x";
echo $$var;
}

Variable variables. Your example could be used like so:
$varname_0 = 'Hello World!';
$x = 0;
echo ${'varname_'.$x}; // 'Hello World!'

Related

How do I return a hash object from a function in PHP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to do something that I'm sure is simple but I can't gather how to do it from the documentation.
All I want to do is:
function something() {
// Do some stuff with cURL
set $object->name;
set $object->corporation;
// some if statement
set $object->other; //if cURL finds "other" exists
return $object;
}
And be able to do this:
$result = something();
$name = $result->name;
etc.
Does anyone know the best way to do this? I use procedural style and have very little know-how with objects hence I'm trying to learn.
function foo(){
$result = new stdClass();
$result->name = "Joe";
$result->other = "bar";
return $result;
}
$object = foo();
echo $object->name; // Should display "Joe"

php initialize if not set [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find myself doing the following quite a bit:
if (isset($objs[$key]){
$objs[$key] += val;
} else {
$objs[$key] = $val;
}
Is there a better way to do that?
Another way could be:
$objs[$key] = isset($objs[$key]) ? $objs[$key] + $val : $val
Is there a better way?
The easiest way to simplify could be a function taking the array by reference and deduplicate the code.
If you are using php 7 you can also make use the new ?? operator (assuming all values are integers):
$arr[$key] = ($arr[$key] ?? 0) + $value;
However, if you know the shape of the array you should base your actions on that. Create the array using array_fill_keys or similiar in that case.
If you are always adding numbers... you could do something like this to keep a single line of code for each entry which may or may not be preferable for you to your existing solution.
$objs = array();
addvalue($objs, "dog", 2);
addvalue($objs, "dog", 5);
// $objs['dog'] will = 7
function addvalue(& $var, $key, $value){
if (isset($var[$key])){
$var[$key] += $value;
} else {
$var[$key] = $value;
}
}

Variable containing loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am not sure if this is even possible or if it makes sense but I want to set a variable to contain this loop:
<?php
do{
$x = $x + 1;
} while ($x<=5);
?>
Can this be accomplished with a variable variable?
I suspect you're looking to create a function:
function fun($x) {
do {
$x = $x + 1;
} while ($x <= 5);
return $x;
}
Then in your code you can simply write:
$x = fun($x);
Do you mean like this?
<?php
$varVar = rand(0,10);
for ($i=1; $i<=$varVar; $i++)
{/*the loop*/}
?>
I'm not really sure what you mean, unless you're after something similar to the block syntax of Objective-C?
I'm presuming you're new to PHP, and if so, are you sure you're not looking for functions? What you've done above will make the value of $x equal 5, if you wanted to do this with a function it would look like:
<?php
function doSomething($count) {
do {
// do something here
} while ($x <= $count);
}
doSomething(5);
?>

PHP shorthand (Ternary) function [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In the javascript world we can run a function through a Ternary comparison, I wanted to see if this applies to PHP, it seems it does only to an extent.
This is not for production use nor will it be actually used. This is merely a topic to see PHP's in-depth extent of the ternary comparison.
<?
$a = 1;
$b = 1;
function doThis($a){
print "$a";
}
$a == $b ? ( doThis('TRUE') ):( print "FALSE" );
?>
The above code works perfectly, however, is it possible to run multiple functions and or operations within ()?
Such as?
$a == $b ? ( doThis('TRUE'), doThis('THAT') ):( print "FALSE" );
or even?
$a == $b ? ( function(){ print "33"; doThis("TRUE") } ):( print "FALSE" );
You can have the ternary return a closure that would perform the requested function
$func = $a==$b?function(){ print "33"; doThis("TRUE"); }:function(){ print "FALSE"}); );
$func();
or borrowing from javascript you can create a IIFE (Immediately Invoked Function Expression)
$a==$b?call_user_func(function(){print "33"; doThis("TRUE");}):
call_user_func(function(){print "FALSE"; });

Error on line that doesnt exist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.

Categories