0

SCROLL DOWN if you just want to see the question without much explanation

I have kind of a big application with a lot of values that are used as options. The control for the application is a website, application itself is C++ and all the options are stored inside MySQL so that both C++ and php can access it. So I am using php to read bunch of values from a text file, and auto generate SQL, C++ and php code, since the options are used everywhere and are so massive that it would take too long to implement them, and would be too much of a hassle to add an extra options in the future.

This is what one line of my text file looks like

`input_video_standard`|:|INT|:|NOT NULL DEFAULT '0'|:|profile->input.videoStandard|:|Video Standard|:|SD 576i@50Hz (B,G/PAL)|:|SD [email protected] (NTSC)|:|HD 720p@50Hz|:|HD [email protected]|:|HD 1080i@25Hz|:|HD [email protected]

Once I read in that line I use

$pieces = explode("|:|", $line);

This part is for generating C++ code now I have an array called variables, where I store name of the variable for C++ and what type it is.

$variables[] = array($pieces[1], $pieces[3]);

Once I loop through the text file and gather up all the data

I try to create a string which I will later on store inside a text file

$cpp .= "memset(prof,0,sizeof(prof));\n";
$cpp .= "sprintf(prof, \"CALL put_into_input(%d, 'test'";
for($i = 3; $i<count($variables); $i++)
{
    if(strpos($variables[0], "VARCHAR") === FALSE)
    {
        $cpp .= ", %d";
    }
    else
    {
        $cpp .= ", '%s'";
    }
}
$cpp .= "\nprofile-".">"."pfile";
for($i = 3; $i<count($variables); $i++)
{
    $cpp .= ",\n".$variables[1];
}
$cpp .= ");\n";

THE QUESTION

The problem is that when I try to concatenate string profile->input.videoStandard to my main string it simply saves it as Array, and I have noticed that php does not like -> as a string in general, which is why I used "-".">" wherever I could. How do I go about getting php of accepting -> as a string?

Thanks to anyone for their help.

2
  • How do You concatenate the string? Commented Nov 2, 2011 at 13:18
  • I use . to concatenate strings. Commented Nov 2, 2011 at 14:50

3 Answers 3

2

I have noticed that php does not like -> as a string in general.

Nothing wrong with a -> in a string.

Unless there is some $ in there too.

This will cause PHP to think you are trying to access a property of an object instance.

E.g. $object->something. You could just use single quotes in stead of double quotes to prevent PHP trying to parse the string. Other than that there shouldn't be any problem using -> in a string.

EDIT

You're code looks strange to me.

You are doing $variables[] = array($pieces[1], $pieces[3]);

which is the same as $variables = array(array($pieces[1], $pieces[3]));

So you only have $variables[0] and not $variables[1]

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you very much, that has worked! But what about reading in the string from a textfile? I can not control whether it has double or single quotes. So how would I go around that? The string being read in is still being displayed as Array instead of profile->input.videoStandard
@Quillion: Have you changed all your double quotes to single quotes around your string in your PHP file?
Yes every single one besides newline character, so if I want a newline character I do this ');'."\n"; but other than that every single one is a single quote. But my string that is being faulty is stored inside $variables[1] which I read from the text file, and I concatenate it like this $cpp .= ','."\n".$variables[1]; could this be the problem?
Can you do a var_dump($variables[1]) or print_r($variables[1]); and report the result?
I did that. when I print the value of $pieces[3] it says profile->input.videoStandard When I do $variables[] = array($pieces[1], $pieces[3]); Then do print_r it shows that it is an array that has INT or VARCHAR for first value and "" for second. Does the string somehow dissapear?
|
1

In PHP, variables within double quotes are parsed.

$var = "hello";
$str = "This is a variable: $var";

// The value of $str will be "This is a variable: hello"

When you use single quotes variables are not parsed.

$var = "hello";
$str = 'This is a variable: $var'; // Please note the single quotes here

// The value of $str will be "This is a variable: $var"

I think in your case, the -> are recognised as variables because that's the syntax used for object-oriented programming to get both properties and methods.

$object->property;
$object->method();

Personally I use single quotes whenever I don't need my string to be parsed for variables or regular expressions.

Single quote strings

Double quote strings

Comments

-2

You could use apostrophes intead of quotes maybe ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.