0

I want to generate connector.php file during installation like this

$file = "connector.php";
$fp = fopen($file,"wb");

$content = "<?php $con=mysqli_connect('";
$content += $host;
$content += "', '";
$content += $user;
$content += "', '";
$content += $password;
$content += "', 'blog');  if (mysqli_connect_errno()) { echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); }?>";

fwrite($fp,$content);
fclose($fp); 

but when I try this code, I just generate empty file (when I try some random text, it works fine), so I think, the problem is, that PHP trying to interpret everything between <?php and ?>. How can I make this code work, and make PHP not interpret code between php tags.

6
  • ...and just why are you dynamically generating a php file for a database when you could just have a config file and require_once() it? Commented Aug 11, 2013 at 19:59
  • When I try to generate file only with '<?php', I just generate empty file as wel Commented Aug 11, 2013 at 19:59
  • @Hiroto I am trying to create automated installation Commented Aug 11, 2013 at 20:00
  • ...no, really, why are you generating a file in the first place? This is not how you automate installation of things in PHP. Generate a config file if needed, but don't dynamically generate code unless you know exactly what you are doing. Commented Aug 11, 2013 at 20:02
  • 1
    Hint: this is not JavaScript ! Commented Aug 11, 2013 at 20:02

2 Answers 2

4

Use .= as concatenation operator.

Using + probably converts the whole thing to an integer. You also have to use single quotes around the first line in the second code block because PHP tries to interpret the undefined variable $con otherwise.

$file = "connector.php";
$fp = fopen($file,"wb");

// Use single quotes here, otherwise PHP tries to interpret $con
$content = '<?php $con=mysqli_connect(\'';
$content .= $host;
$content .= "', '";
$content .= $user;
$content .= "', '";
$content .= $password;
$content .= "', 'blog');  if (mysqli_connect_errno()) { echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); }?>";

fwrite($fp,$content);
fclose($fp); 
Sign up to request clarification or add additional context in comments.

5 Comments

Use single quotes in the first line.
Because: $con will be seen as a variable content.
@CasimiretHippolyte Yeah, you're right! Sorry, I thought you meant the very first line $file = "connector".php. I've already corrected it.
@ComFreek +40 -2 + easy answer = who cares :)
@HamZa: nah, it's 50-2. Wrong math! :P
4

+= is a numeric operator and .= is a string operator. You're currently using += in your code and it will cause PHP to interpret it as a number.

The + or += operator first converts the values to integers (and all strings evaluate to zero when cast to ints) and then adds them, so you get 0. That's why your code isn't producing the expected result.

See ComFreek's answer above.

Also, an unrelated issue: to tell PHP not to use the actual variable values in the string, simply escape them, like so:

$content = "<?php \$con=mysqli_connect('";

Or, you can use single quotes:

$content = '<?php $con=mysqli_connect(\'';

reference: #2202331

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.