Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set variables in a php include file?

I have a set of variables that are set outside the target php include file, how would i set them in the target php include file. Example:

<?php
  $fname = "david";
?>

Now how would I set $fname in another php file?

like image 600
dave Avatar asked Nov 30 '25 01:11

dave


1 Answers

In your other PHP file you would do:

<?php
  $fname = "david";
?>

This answers your question directly, but I would hazard a guess that you actually want to have access to variables that are set in a file that is included into your current file or something along those lines.

So

File1.php:

<?php
  $fname = "david";
?>

File2.php

<?php
require_once 'File1.php';
echo $fname;

Would result in david being printed to screen.

like image 143
Treffynnon Avatar answered Dec 02 '25 15:12

Treffynnon