1

I get this error when I try and include a variable from an external php file: Unexpected T_VAR on line 1

Here is my external php code:

<?php var dbStr = 'host::,username::,password::,database'; ?>

Here is the PHP file that is including it:

if($_POST['admincreate'] == "ok")
{
include('db.php');
$info = explode("::,", dbStr);
$con=mysqli_connect($info[0],$info[1],$info[2],$info[3]);
mysqli_query($con,"INSERT INTO admins (id, user, pass)
VALUES ('" . $_POST['user'] . "', '" . $_POST['pass'] . "',0)");
}

It works if I change the code to

if($_POST['admincreate'] == "ok")
{
$dbStr = 'host::,username::,password::,database';
$info = explode("::,", $dbStr);
$con=mysqli_connect($info[0],$info[1],$info[2],$info[3]);
mysqli_query($con,"INSERT INTO admins (id, user, pass)
VALUES ('" . $_POST['user'] . "', '" . $_POST['pass'] . "',0)");
}

But I need the included file.

If I change the included file to

<?php $dbStr = 'host::,username::,password::,database'; ?>

I get this error: Parse error: syntax error, unexpected '=' in .../db.php on line 1

And I find that the external php file has changed to

<?php  = 'host::,username::,password::,database'; ?>

How do I include this string properly without it giving me an error?

P.S. The external php is generated by the main php file use fopen and fwrite, also the actual values of host, username, password, and database have been censored just because I feel better that way.

Thanks in advance, -p0iz0n

4

1 回答 1

5

var不是有效的 PHP

<?php var dbStr = 'host::,username::,password::,database'; ?>

应该

<?php $dbStr = 'host::,username::,password::,database'; ?>

有趣的是,您在第二个示例中就正确了。

于 2013-05-05T23:36:18.243 回答