Jump to content

PHP script writing a PHP script

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
I'm trying to write a script from within a script. The attached code works, however, the variable $line4 needs to be "$_SESSION['pic_Index']='10030';.

If I include the "$" in front of "_SESSION..." I get a program error. Is there anyway to write "$_SESSION..." to the file without getting an error?

<?php

Echo "Write file.........";

// write first file

$filename = 'olgtemp1.php';

$open_it = fopen($filename, 'w+') or die("Can't open file");

$line1 = "<?php \n";

$rc = fwrite($open_it, $line1);

$line2 = "session_start(); \n";

$rc = fwrite($open_it, $line2);

$line3 = "//Temp file created by olg13.php for use by olglistpics.php \n";

$rc = fwrite($open_it, $line3);


$line4 = "_SESSION['pic_Index']='10030'; \n";

$rc = fwrite($open_it, $line4);


$line5 = "include('olglistpics.php'); \n";

$rc = fwrite($open_it, $line5);

$line6 = "?>";

$rc = fwrite($open_it, $line6);

fclose($open_it);

?>

Thanks,
Jon

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the easiest way is to embrace the line with apostrophes (') instead, as the string is not parsed before usage then, and change the apostrophes inside to quotation marks

$line4 = '$_SESSION["pic_Index"]="10030"; \n'; 


#3
ezcat

ezcat

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Thank you for explaining the difference between (') and ("). I did not know and is a good lesson learned.

Thank you,
Jon

#4
thetit4n

thetit4n

    Newbie

  • Members
  • PipPip
  • 15 posts
There are also some situations where that won't work. Like if you need to include a " ' " for example. In that case you can escape the character with a "\". This will make the PHP processor see the character as text and not PHP.

for example you could do


<?php


$string = "\$_SESSION[\'foo\'] = \"bar\";"


?>




#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
although much easier to prevent parsing the string by using ' instead of " in this specific cases. I don't know anywhere in php you can't change between apostrophes and quotation marks without problems, unless you want to php-parse the string.

for calling a specific post in an array, there's not even need for any marks
$test[var], $test['var'], $test["var"] are equivalent in this case.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
thetit4n

thetit4n

    Newbie

  • Members
  • PipPip
  • 15 posts
What if you wanted to do this?



echo "And mike said "$var['foo']" to roger.";



You would have to escape them or use curly braces. I wasn't trying to say you were wrong I just felt he should know the alternative in case he ever came across a situation that he needed it.

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
what about
echo 'And mike said "$var[foo]" to roger.';
that is if you want to echo the variable name. if you want to parse the variable name,
echo 'And mike said "'.$var[foo].'" to roger.';
is easier to write, read and understand.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall