0

I have a variable variable defined, suppose:

$array[1] = Hello World;
$i = 1;
${"name" . $i} = $array[1];

I then have the following:

$val1 = "Hello World";

However when I try to compare $name1 to $val1, they are never equal.
I've tried all of the following:

if ($name1 == $val1){
    echo 'yes';
}
else{
    echo 'no';
}
//second try
if ($name1 === $val1){
    echo 'yes';
}
else{
    echo 'no';
}

//3'rd attempt

if((string)$name1 === $val1){
    echo 'yes';
}
else{
    echo 'no';
}

All print out no
Any ideas?

3
Contributors
9
Replies
13
Views
5 Years
Discussion Span
Last Post by persianprez
0

I've tried that, however the array is actually being read in through a text file
do you think if I put quotes around each line in the file, it would work?

Edited by persianprez

0
$filename = dirname(__FILE__) . "/raw_data.txt";

$fp = @fopen("$filename", 'a+');
if ($fp){
    $array = explode("(.)", fread($fp, filesize($filename)));
}
0

It works for me, try to use var_dump on $name1: var_dump($name1); if Hello World is the first of the array, then the problem is this line ${"name" . $i} = $array[1]; because the array starts from 0, using $array[1] in my example $name1 will print How Are You?, so try to change it to ${"name" . $i} = $array[0];. If you are going to loop then just switch $i to 0.

This is what I've done, raw_data.txt example:

Hello World(.)How Are You?(.)Bless this mess!

script:

<?php
$filename = dirname(__FILE__) . "/raw_data.txt";

$fp = @fopen("$filename", 'a+');
if ($fp){
    $array = explode("(.)", fread($fp, filesize($filename)));
}

var_dump($array);
$i = 1;
${"name" . $i} = $array[0];
$val1 = "Hello World";

if ($name1 == $val1){
    echo 'yes' . "\n";
}
else{
    echo 'no' . "\n";
}
//second try
if ($name1 === $val1){
    echo 'yes' . "\n";
}
else{
    echo 'no' . "\n";
}

//3'rd attempt

if((string)$name1 === $val1){
    echo 'yes' . "\n";
}
else{
    echo 'no' . "\n";
}

var_dump($name1);

?>

will output:

array(3) {
  [0]=>
  string(11) "Hello World"
  [1]=>
  string(12) "How Are You?"
  [2]=>
  string(16) "Bless this mess!"
}
yes
yes
yes
string(11) "Hello World"

Edited by cereal

0

I havent tried using var_dump yet, but I'll try it once I'm free. For now, I will post the following:

To make my problem easier to figure out, I'll just post my entire code:apps.php

$category = "Apps";

$filename = dirname(__FILE__) . "/raw_data.txt";

$fp = @fopen("$filename", 'a+');
if ($fp){
    $array = explode("(.)", fread($fp, filesize($filename)));
}
$r = sizeof($array);
for($z=0; $z<$r; $z++){
for ($i=1; $i<=15; $i++){


        ${"name" . $i} = $array[$z];
        $z++;
        ${"author" . $i} = $array[$z];
        $z++;
        ${"version" . $i} = $array[$z];
        $z++;
        ${"icon" . $i} = $array[$z];
        $z++;
        ${"linkto" . $i} = $array[$z];
        $z++;
        ${"description" . $i} = $array[$z];
        $z++;
        ${"screenshot" . $i} = $array[$z];
        $z++;
        ${"size" . $i} = $array[$z];
        $z++;
        ${"original" . $i} = $array[$z];/**/
        $z++;




}}

an example of what is in raw_data.txt

MPC Remote(.)
m0sand(.)
1(.)
http://i.imgur.com/eNRmZ.png(.)
http://adf.ly/CapXX(.)
A small app for controlling a media player classic instance running on a computer. Features a clean interface (nothing too flashy atm) Basic playback controls such as Play/Pause, Next, Previos, Volume and seeking controls.(.)
http://i.imgur.com/IGkQ5.png(.)
206.6KB(.)
http://forum.xda-developers.com/showthread.php?t=972616(.)

The comparison:

if ($val == $name1){
    $name = $name1;
    $author = $author1;
    $version = $version1;
    $icon = $icon1;
    $linkto = $linkto1;
    $description = $description1;
    $screenshot = $screenshot1;
    $size = $size1;
    $original = $original1;
}
0

Ok, you can also add trim() to $name1: ${"name" . $i} = trim($array[$z]); and you can do the same with $val1.

1

@cereal
trim() worked great, thanks so much!
If you would like to see what I'm working on, go to www.pedrumgolriz.com/xaptrack
Thank again!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.