Insert php inside double quotes

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Sefanjo
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Jun 06, 2018 1:16 pm

Wed Jun 06, 2018 1:26 pm

Hi guys,

I hope someone can help me out,
I'm trying to make an sort of CRUD system.

When I am creating a new item I want it to make a new .php file with the given title.
So I tried it myself first, with some help of w3school.

This is the code I have right now:

Code: Select all

$myfile = fopen("<?php echo $pagetitle;?>.php", "w") or die("Unable to open file!");
            $txt = "test\n";
            fwrite($myfile, $txt);
            $txt = "test\n";
            fwrite($myfile, $txt);
            fclose($myfile);
 
The problem that I have is that the file isn't getting the name I want it to have, that php 'function' inside the double quotes is not acting like php. It just is outputting the text.
I've checked some of my other codes and there I am able to put php inside the double quotes.

So, does anyone know how I can fix this?

chorn
php-forum Fan User
php-forum Fan User
Posts: 558
Joined: Fri Apr 01, 2016 2:18 am

Wed Jun 06, 2018 9:37 pm

Only variables are interpreted within double-quotes - php code is not. And fopen requires just a filename, like "phpwtf.txt", that may be hold within a string, but also filesystem restrictions apply (e.g. no slashes, except for if you want to create a directory).

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Thu Jun 07, 2018 10:23 am

Try changing your code to this:

Code: Select all


$myfile = fopen($pagetitle .".php", "w") or die("Unable to open file!");
            $txt = "test\n";
            fwrite($myfile, $txt);
            $txt = "test\n";
            fwrite($myfile, $txt);
            fclose($myfile);
 


Post Reply