echo variable with variable in included.php - php

I have two php files.
One is action.php and the other is include.php
I need to echo variable form include.php in my other scripts.
Basically in include.php there are this variables
$id= $web_id;
$start = "Hello, ";
$end = "works.";
In my action.php I have this variables
$web_id = "testing echo from variable in include.php";
echo $start; // variable is in include.php
echo $id; //variable is in include.php
echo $end; //variable is in include.php
Result is
Hello, works.
Wham I am trying to achieve is
Hello, testing echo from variable in include.php works.
This syntax echos only empty value for $id.
What is the right syntax for echoing variable with variable in included file?

You need to set your $web_id variable before you do your include. Take a look at this example:
action.php
$web_id = 'this'.
include 'include.php';
echo $start . $id . $end;
include.php
$id = $web_id;
$start = 'Hello ';
$end = ' works.';

Related

How to comment in and out php code inside a file

Using PHP how to comment in all php code inside certain php file
for example if i've the followig file
$file = 'myfile.php';
has only PHP code
<?php
$c = 'anything';
echo $c;
?>
I want using PHP to comment in (add /* just after open tag <?php and */ just before close tag ?>) to be
<?php
/*
$c = 'anything';
echo $c;
*/
?>
And also how to do the reverse bycomment out (remove /* */) to return back to
<?php
$c = 'anything';
echo $c;
?>
I've been thinking to use array_splice then doing str_replace then using implode and file_put_contents but still unable to figure out how to do this.
Update
Okay meanwhile getting some help over here, i was thinking about it and it comes to my mind this idea .... USING ARRAY!
to add block comment /* just after open tag <?php i will convert the content of that file into array
$contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
and then i can array push new element at position 2 with /*
and to do the reverse i will use unset($contents[1]); to unset element at postion 2 which means, /* will be gone
later on i can file_put_contents($file, $contents); to re-write the file again.
You can use PREG_REPLACE :
<?php
function uncomment($file_path) {
$current = file_get_contents($file_path);
$current = preg_replace('/\\/\\*(.+?)\\*\\//s', '$1', $current);
file_put_contents($file_path, $current);
return $current;
}
echo "<plaintext>" . uncomment("code.php");
?>
BEFORE :
AFTER :
I don't know why you want comment or uncomment the php code but I don't think it's a good way to do. I advice you to use variable or constant, like this :
One other way to enable or disable you code, is to use constant variable after the second time :
TOGGLE/UNTOGGLE COMMENT :
You will be able to do :
uncomment("code.php", "MYENV_DEBUG"); // uncomment
uncomment("code.php", "MYENV_DEBUG"); // comment
uncomment("code.php", "MYENV_DEBUG"); // uncomment
uncomment("code.php", "MYENV_DEBUG"); // comment
FIRST TIME :
SECOND TIME :
THIRD TIME :
Code :
<?php
function uncomment_header($name, $value) {
return '<?php define("' . $name . '", ' . $value . '); ?>';
}
function uncomment($file_path, $name) {
$current = file_get_contents($file_path);
$regex = '/<\\?php define\\("' . $name . '", (0|1)\\); \\?>/';
if (preg_match($regex, $current, $match)) {
$value = ($match[1] == 1) ? 0 : 1;
$current = preg_replace($regex, uncomment_header($name, $value), $current);
} else {
$header = uncomment_header($name, 1) . "\n";
$start = 'if (' . $name . '):';
$end = 'endif;';
$current = $header . $current;
$current = preg_replace('/\\/\\*(.+?)\\*\\//s', $start . '$1' . $end, $current);
}
file_put_contents($file_path, $current);
return $current;
}
echo "<plaintext>" . uncomment("code.php", "MYENV_DEBUG");
?>
There are two types of comments in PHP 1)single line comment 2)Multiple line comment for single comment in php we just type // or # all text to the right will be ignored by PHP interpreter. for example
<?php
echo "code in PHP!"; // This will print out Hello World!
?>
Result: code in PHP!
For multiple line comments multiple line PHP comment begins with " /* " and ends with " / " for example
<?php
/* This Echo statement will print out my message to the
the place in which I reside on. In other words, the World. */
echo "Hello World!";
/* echo "My name is Noman Ali!";
echo "PHP Programmer!";
*/?>
Result: Hello World!
Like this:
#canned test data, a string and the file contents are the same
$contents = <<<'CODE'
<?php
$c = 'anything';
echo $c;
?>
CODE;
$contents = preg_replace(['/<\?php\s/','/\?\>/'], ['<?php/*', '*/?>'], $contents);
echo $contents;
Output
<?php/*
$c = 'anything';
echo $c;
*/?>
Sandbox
NOTE - this will only work if the ending tag is present. In PHP the ending tag is actually optional. This will also not work on things like short tags <? or <?= although it will catch the ending tags.
Because of these edge cases it's very hard to do with regex (or any string replacement).
Valid examples of PHP code
<?php
$c = 'anything';
echo $c;
?>
//-------- no ending tag ---------
<?php
$c = 'anything';
echo $c;
//------- short tags ---------
<? echo 'foo'; ?>
//------- short echo tags ---------
<?= $foo; ?>
etc...
Good luck if you want to try to catch them all....

how to use variable variables PHP function on my code?

Normally i use this code and it's will be echo work good.
<?PHP
echo $row->test_column_1;
?>
But when i tyied to use this code, it's not echo any data. How can i do ?
<?PHP
$i = "1";
echo ${'row->test_column_' . $i};
?>
You can access a dynamic property name like this:
<?php
$i = "1";
echo $row->{'test_column_' . $i};

How to combine two PHP variable names to call one?

I have multiple variables:
$Variable1 = '5/5/15';
$Variable2 = '6/13/76';
$Variable3 = '5/8/15';
...
I have an iteration variable:
$Iteration1 = 1;
while($Iteration1<=3){
echo "$Variable" . $Iteration1;
$Iteration1++;
}
desired result:
5/5/15
6/13/76
5/8/15
The Problem
Your current code echo "$Variable" . $Iteration1; tries to echo the value of the variable $Variable, which doesn't exist, and concatenate $Iteration1.
The Solution
What you want to do is build a string, "Variable" . $Iteration1 (e.g., $Variable2), then get the value of the variable with that name. This is called a "variable variable." You do this by writing ${string_you_want_to_create}, as in ${"Variable" . $Iteration1}.
Example Code For your Problem:
$Variable1 = '5/5/15';
$Variable2 = '6/13/76';
$Variable3 = '5/8/15';
$Iteration1 = 1;
while ($Iteration1 <= 3) {
echo ${"Variable" . $Iteration1} . "\n";
$Iteration1++;
}
Output:
5/5/15
6/13/76
5/8/15
Note: You could also do this in two steps, like this:
$variableName = "Variable" . $Iteration1;
echo $$variableName; // note the double $$
Try like this
echo ${"Variable" . $Iteration1};
Try this in loop
$var = 'Variable'.$Iteration1;
echo $$var.'<br>';

Function doesn't use variables defined above it

I have a variable named $url in the top of my file:
<?php
$url = "http://myurl.com";
Later in the same file, I have this code:
<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
echo $url;
}
?>
However, that doesn't work, because it says $url isn't a valid variable. I have to do this:
<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
$url = "http://myurl.com";
echo $url;
}
?>
This doesn't make any sense to me because it shouldn't be out of scope because it's a layer above the function. How do I make it use the earlier $url variable?
They are not in the same scope. You have to let PHP know you will be using that global locally. It is preferable to not use a global and instead pass it as a variable though.
<?php
$url = "http://myurl.com";
[...]
function errorOut($error, $type = "info", $rel = "/")
{
global $url;
echo $url;
}
?>
See Variable scope for more information.
I was talking to someone in an IRC about this (he posted on here too before I joined), he said I should use
define("URL", 'http://example.com');
And whenever I reference that variable I should use URL, not $url
by passing the $url in your function:
function errorOut($error, $type = "info", $rel = "/", $url) //<<< here
and also calling it:
errorOut('...','...','...',$url);
NON WORKING EXAMPLE AS SEEN IN YOUR ANSWER
$a = 'test1';
$b = 'test2';
define ('URL','one');
define ('URL', 'two');
test($a,$b);
function test ($a,$b){
echo $a;
echo $b;
echo URL;
}
Won't work, URL will stay at 'one' // Will only work if you never want to change URL

View PHP code after includes

Basically, I need to view the PHP code of a file, after includes. I am trying to see EXACTLY what PHP code is run. eg...
<?php // a.php
$a = 10;
?>
<?php // b.php
include('a.php');
$b = 20;
?>
If I was trying to get the code of b.php, it would display the following:
<?php
$a = 10;
$b = 20;
?>
Is that possible? If so, how?
// at the end of your script
<?php
$totalRunCode = '';
$scripts = get_included_files();
foreach($scripts as $script)
{
$totalRunCode .= file_get_contents($script);
}
// do whatever with totalRunCode
Though I don't know why you'd want to do this.

Categories