<?php
$dog[] = "12";
$dog[] = "3";
for ($i = 0; $i < 2; $i++) {
$dig = $dog[i];
echo $dig;
}
?>
$dig is always null. Why?
i is not a variable, use $i
If you had error_reporting(E_ALL) on, as you should when in development, you would have caught it immediately (undefined constant).
$dig = $dog[i];
should be:
$dig = $dog[$i];
Easy. You want $dog[$i]. The PHP engine looks for a constant name i, can't find one, so resorts to looking for string. No a key with value 'i' either, so returns NULL.
your missing the $ in this line
$dig = $dog[i];
should be
$dig = $dog[$i];
you could also simplify this code by writing it this way
<?php
$dogs[] = "12";
$dogs[] = "3";
foreach($dogs as $dog) {
echo $dog;
}
?>
You want
$dig = $dog[$i];
You missed the $
Related
With this code:
$guessString = 'KUĆA';
$usedLetters = ['Ć'];
$userLetter = 'Ć';
for ($i = 0; $i < mb_strlen($guessString); $i++) {
$temp = $guessString[$i];
if (in_array($guessString[$i], $usedLetters)) {
echo $guessString[$i];
} else {
echo ' _ ';
}
}
I am trying to compare if $userLetter (that is sent through GET link)
exists in $guessString.
But through debugging:
I finally see that it doesn't read the character right.
It gets "?" instead of "Ć"
So my question is: How to get the correct character with for loop ($guessString[$i])?
Your problem is with
$guessString[$i]
The indexing operator is not multibyte-aware, so it returns the $ith byte of the string. One way to fix this is using mb_substr:
$temp = mb_substr($guessString, $i, 1);
Of course, you should then use $temp everywhere you used $guessString[$i].
At first glance I think you can get what I'm trying to do. I want to loop though variables with the same name but with a numerical prefix. I also had some confusion about the kind of loop I should use, not sure if a "for" loop would work. The only thing is I can't wrap my head around how php could interpret "on the fly" or fabricated variable. Ran into some trouble with outputting a string with a dollar sign as well. Thanks in advance!
$hello1 = "hello1";
$hello2 = "hello2";
$hello3 = "hello3";
$hello4 = "hello4";
$hello5 = "hello5";
$hello6 = "hello6";
$hello7 = "hello7";
$hello8 = "hello8";
$hello9 = "hello9";
$hello10 = "hello10";
for ( $counter = 1; $counter <= 10; $counter += 1) {
echo $hello . $counter . "<br>";
}
It's generally frowned upon, since it makes code much harder to read and follow, but you can actually use one variable's value as another variable's name:
$foo = "bar";
$baz = "foo";
echo $$baz; // will print "bar"
$foofoo = "qux";
echo ${$baz . 'foo'}; // will print "qux"
For more info, see the PHP documentation on variable Variables.
However, as I already mentioned, this can lead to some very difficult-to-read code. Are you sure that you couldn't just use an array instead?
$hello = array(
"hello1",
"hello2",
// ... etc
);
foreach($hello as $item) {
echo $item . "<br>";
}
Try ${"hello" . $counter}
$a = "hell";
$b = "o";
$hello = "world";
echo ${$a . $b};
// output: world
You can use variable variables as:
for ( $counter = 1; $counter <= 10; $counter += 1) {
echo ${'hello' . $counter } , '<br>';
}
as I guess u not even need to declare $hello1 = "hello1". coz the $counter is incrementing the numbers by its loop.
<?php
for ( $counter = 1; $counter <= 10; $counter += 1) {
echo 'hello' . $counter . "\n";
}
?>
so this is enough to get the output as you want.
the output will be:-
hello1
hello2
hello3
hello4
hello5
hello6
hello7
etc...
So i want to deo something like this and not sure how
for($s=0; $s < 5; $s++ ){
$pre_config_query = "select * from preconfig where code = '{$industry_string}_{$s}_{$class_string}'";
$pre_config_station = mysql_query($pre_config_query);
$it_exists = mysql_num_rows($pre_config_station);
if($it_exists>0){
$pre_config = mysql_fetch_assoc($pre_config_station);
$pre{$s} = $pre_config['id'];
I want the end product to have these 5 variables named
print $pre1;
print $pre2;
print $pre3;
print $pre4;
print $pre5;
That have the $pre_config['id'] if present....any ideas
You can use variable variables to accomplish that.
First, define a variable with the desired name:
$varname = "pre$s";
Second, assign a value to it:
$$varname = $pre_config['id'];
That's all!
this works but I'm not sure I'm answering your question.
<?php
for($s=1; $s < 6; $s++ ){
$it_exists=1;
if($it_exists > 0){
$pre_config = array('id'=>rand(10,99));
${"pre".$s} = $pre_config['id'];
}
}
echo $pre1."<br/>";
echo $pre2."<br/>";
echo $pre3."<br/>";
echo $pre4."<br/>";
echo $pre5."<br/>";
?>
look at this simple script please
$c1 = $_GET[c1];
$c2 = $_GET[c2];
$c3 = $_GET[c3];
$c4 = $_GET[c4];
$c5 = $_GET[c5];
for($i = 1;$i <=5;$i++)
{
echo $c{$i};//or something else here :/
}
how can i print tha values of variables?
Thanks
You can see on php.net some good examples in the variable page. Read that and take a look at the examples.
Also, below is your piece of code fixed so it can work:
<?php
$c1 = $_GET[c1];
$c2 = $_GET[c2];
$c3 = $_GET[c3];
$c4 = $_GET[c4];
$c5 = $_GET[c5];
for($i = 1;$i <=5;$i++)
{
echo ${"c".$i};
}
You should use an array rather than individual variables.
For reference:
http://php.net/manual/en/language.types.array.php
If these values are closely related, consider changing their name attribute in your HTML/form.
HTML:
<form>
<input type="text" name="c[]" />
<input type="text" name="c[]" />
...
</form>
PHP:
<?php
if(!empty($_GET['c'])) {
foreach($_GET['c'] as $c) {
echo $c;
}
}
?>
Here's a better way of doing it, using Arrays, rather than individual variables, which works easier and more efficiently.
<?php
$array['c1'] = $_GET['c1'];
$array['c2'] = $_GET['c2'];
$array['c3'] = $_GET['c3'];
$array['c4'] = $_GET['c4'];
$array['c5'] = $_GET['c5'];
for ($i=1; $i>=5; $i++) {
echo $array['c' . $i];
}
?>
Perhaps PHP variable variables is what you are looking for.
$i = "c1";
print $$i;
I'll leave it to you to figure out how to construct correct values for 'i'.
This should work..
foreach($_GET as $id => $value){
echo $value;
}
even though this prints out every $_GET.
there are 10 variables. say $var1, $var2, $var3, $var4,....$var10
and a $count variable. what I am looking for is if all variables are set then $count = 10+1 or if 9 variables only set then $count=9+1 or if 8 variables only set then $count=8+1 and so on last up to 1 variable(for one variable is set then $count = 1+1).
I know do this with If, Else if and else but I need to write too much line of code.
Does any one know how to do this in brief code??
You can achieve this using variable variables in PHP:
$count = 1;
for ($i = 1; $i <= 10; $i++)
{
if ( isset( ${'var'.$i} ) ) $count++;
}
Try this (untested):
$arr = array($var1, $var2, $var3, $var4, $var5, $var6, $var7, $var8, $var9, $var10);
$count = 1;
foreach ($arr as $v)
{
if (isset($v))
$count++;
}
<?php
...
$countarr = compact('var1', 'var2', ..., 'var10');
$count = count($countarr) + 1;
?>
Er... Are you aware that PHP allows to use loops...
<?php
$var1 = 100;
$var4 = 200;
$var9 = 600;
$count = 0;
for($i=1; $i<=10; $i++){
if( isset(${"var$i"}) ){
$count++;
}
}
echo $count;
?>
... and arrays?
<?php
$var = array(100, 200, 600);
$count = count($var);
echo $count;
?>
Answer to Boreadlid's remarks
You should note that unsetting an array element is not the same as setting it to NULL. This code:
<?php
$data = array(10, 20, 30);
$data[1] = NULL;
unset($data[0]);
var_dump($data);
?>
... prints this:
array(2) {
[1]=>
NULL
[2]=>
int(30)
}
... so count($data) prints 2 because it is a two element array.
Whatever the OP is trying to accomplish, he should probably be using arrays where unused values are never set to begin with. The $var1, $var2, $var3 approach will work but it's not what a skilled programmer would do. (Of course, there's nothing wrong in being a newbie.)
oh c'mon … you can do a little bit of thinking (and reading documentation) yourself.
simply take the code answered to your previous question, and use isset(${var+$i}, ${tvar+$i}) in your loop
you have 20 vars (10x "var" and 10x "tvar"):
bool[] vars = new bool[10];
bool[] tvars = new bool[10];
i don't get your +1 part in $count... but what about:
int count = 1; //includes the always needed +1 part
for(int i=0;i<10;i++)
if(vars[i] && tvars[i])
count += i+1; //so 0 => 1
??