jared10222
jared10222

Reputation: 35

Need a Way Around PHP Escaping \n

I'm making an install script that dynamically creates an index page for my site. Here is the top portion of my script, up until the error: the line: use menu_manager\navigation\navigation;

$file = '../index.php';
if($handle = fopen($file, 'w')){
    $content = "<?php
ob_start();
 header(\"Cache-Control: no-store, no-cache, must-revalidate, max-age=0\");
 header(\"Cache-Control: post-check=0, pre-check=0\", false);
 header(\"Pragma: no-cache\");

 require_once('inc/functions.php');
 require_once('lib/menu_manager/autoLoader.php');
 use menu_manager\database\database;
 use menu_manager\pagination\pagination;
 use menu_manager\menu\MenuItem;
 use menu_manager\menu\menu;
 use menu_manager\page\page;
 use menu_manager\navigation\navigation;

When I run my script , This is how the actual index file ends up:

use menu_manager
avigation
avigation;

Is there a way to escape the \n in \navigation\navigation so that it does't think I want a new line? Thanks

Upvotes: 1

Views: 48

Answers (2)

Asaph
Asaph

Reputation: 162781

You have 3 options:

  1. Use single quotes instead of double quotes. \n won't be interpolated when in single quotes.

  2. Use nowdoc syntax (since PHP 5.3.0)

  3. Escape the \ in \n with another \. ie. \\n.

Upvotes: 2

Alexander O&#39;Mara
Alexander O&#39;Mara

Reputation: 60527

You need to escape the \ characters with another \ character. See the use statements below.

$file = '../index.php';
if($handle = fopen($file, 'w')){
    $content = "<?php
ob_start();
 header(\"Cache-Control: no-store, no-cache, must-revalidate, max-age=0\");
 header(\"Cache-Control: post-check=0, pre-check=0\", false);
 header(\"Pragma: no-cache\");

 require_once('inc/functions.php');
 require_once('lib/menu_manager/autoLoader.php');
 use menu_manager\\database\\database;
 use menu_manager\\pagination\\pagination;
 use menu_manager\\menu\\MenuItem;
 use menu_manager\\menu\\menu;
 use menu_manager\\page\\page;
 use menu_manager\\navigation\\navigation;

Upvotes: 0

Related Questions