How to call a php function from another php file - php

I have two php files, one index.php
<?php
include ‘Play.php’;
$temp = first(HH);
echo $temp;
?>
second Play.php
<?php
function first($string)
{
return $string;
}
?>
if i remove $temp = first(HH); from the first file then it works, if i include it, the index.php don't work (don't show anything on the screen when i call it within safari.
I know its going to be obvious but what am i doing wrong? Both files are in the same file directory.
Thanks

Use the right kind of quotes around your string for the include. " or ' not ‘ and ’.
The use of typographic quotes like that suggests you may be trying to write code with a word processor. Don't do that; get a text or code editor instead.

Your function is right but the way you are calling your function is not correct.And the way you are including the file is incorrect. You should include your file in quotes like "" or '' . You have to enclose the string in quotes "". Use the code below in index.php
<?php
include 'Play.php';
$temp = first("HH");
echo $temp;
?>
I hope this helps you

include expects double quotes " so change that in, your code and it should work.
include "Play.php";

Kindly change
$temp = first(HH); to $temp = first("HH");
its is because your function tack string as a parameter you should send a string

Related

How to prevent PHP variables and expressions from expanding in string

The following text link works fine when I place it directly in my html:
Click here to <?php echo $showOrHideText; ?> the suggested sequence of lessons.
But I want to replace it with:
<?php echo $gradeNote; ?>
Elsewhere $gradeNote is assigned a string based on the grade of the student user. My question after many hours of searching and failing is how can I pass this snippet as a literal string, without PHP attempting to parse it and giving me a junk url? What am I doing wrong here:
$gradeNote = "Click here to <?php echo $showOrHideText; ?> the suggested sequence of lessons.";
You're running <?php and ?> tags inside of a PHP variable. As you're already dealing with PHP, these are unnecessary.
Although the quotation marks "" allow you to echo out evaluated variables, because you're also running a condition in this 'string', you'll want to extrapolate that out and simply store the result as a variable. I've called mine $show.
As such, you're simply looking for:
if($slcustom29 == 0) {
$show = 1;
}
else {
$show = 0;
}
$gradeNote = "Click here to $showOrHideText the suggested sequence of lessons.";
Remember to either escape the double-quotes in the <a href="">, or swap them for single-quotes.
This can be seen working here.
Hope this helps!
Try something like this.
$s = ($slcustom29 == 0) ? 1 : 0;
$gradeNote = "Click here to {$showOrHideText} the suggested sequence of lessons.";
Any string with double quotes "" can have a variable embedded, the {} are not necessary, but i always use them for cases like this where you are trying to embed a variable with no spaces around it, "$xabc" which will return a different result "{$x}ab"
the probelm is that you are trying to put php logic into the string. Notice you have an IF command within the string literal. start with a small or empty string, and concat onto it piece by piece, as opposed to doing it in one line.
then you can echo out the single variable link

“&” being replaced by "& in php include

I have a php page which get response from another page as shown:
while($response!=200)
{
$response = include 'xyz.php?one='.$one.'&two='.$two.'&three='.$three.'';
}
But my link always get's something like:
domainname.com/xyz.php?one=content&two=content&three=content
And due to & getting replaced by & I am getting the page not found issue.
I have tried using %26 and directly putting & instead of &, all in vain.
Is there any other simple solution besides using string replace function of PHP to remove & and replace it with &
Check out html_entity_decode
$response = html_entity_decode($response)
I ran a test based on the code you sent and I don't have a problem. That suggests you have something auto-magical going on in your *.ini file (magic quotes, maybe... ugh...). Try to create the string simply as a variable to remove it from the filename context and echo it out to be sure it's right, then use the variable with your include.
$one = 'abc';
$two = 'def';
$three = "ghi";
$file= 'xyz.php?one='.$one.'&two='.$two.'&three='.$three;
echo "\n\n".$file;
$response = include $file;
You can't use URL parameters when accessing a local file, they have to go through the webserver. Try:
$response = file_get_contents("http://localhost/path/to/xyz.php?one='.$one.'&two='.$two.'&three='.$three);

Getting a variable from a link PHP

I have two files, one called test3.php, and another called test4.php. I'm trying to echo the variable in the link of the file test4.php, but it's echoing unexpected results. Please take a look.
In the file called test3.php:
<?php
$text = "Good morning.";
header('Location:test4.php?text=$text');
?>
In the file called test4.php:
<?php
$text = $_GET['text'];
echo "$text";
?>
Expected echo result:
"Good morning."
Actual echo result:
$text
I don't understand why it's echoing out $text, instead of "Good morning." One thing that came to mind is that you can't actually set variables when you're using a header, so if that's the case please let me know. Thank you.
Variables do not get parsed in single quotes
header('Location:test4.php?text=$text');
therefore, you need to use double quotes
header("Location:test4.php?text=$text");
References:
https://php.net/language.types.string
https://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
What is the difference between single-quoted and double-quoted strings in PHP?
Plus, it's best to add exit; after header, in order to stop further execution, should you have more code below that (or decide to in the future).
http://php.net/manual/en/function.header.php
and using a full http:// call, as per the manual
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
Footnotes, about header, and as per the manual:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
However you wrote, and I'm using this literally:
Expected echo result:
"Good morning."
If you want to echo just that "Good morning." having the text in double quotes, then you will need to change the following in your test4.php file:
echo "$text";
to, and escaping the " using \
echo "\"$text\"";
use
header("Location:test4.php?text=".$text);
In test4.php:
<?php
$text = $_GET['text'];
echo "$text";
?>
When you quote "$text", you are echoing af string.
What you will want to do, is echo the variable: $text.
So:
<?php
$text = $_GET['text'];
echo $text;
?>
...Without the quotes.. :)
And also, the: header('Location:test4.php?text=$text'); is a bitch, if you use it below a lot of code...
Safe yourself some trouble, and use:
echo "<script type='text/javascript'>window.location.href = 'test4.php?text=".$text."';</script>";
instead ;)

I want to PHP include a variable, doesn't seem to be working

I am trying to PHP include a variable from the database that contains a URL but it doesn't seem to be liking it, any ideas?
The code is
<?php
$location=$row_info['location'];
include '.$location./index.php'; ?>
Thank you for your help :-)
include '.$location./index.php'; ?>
should be
include ".$location./index.php"; ?>
Variable names are not expanded inside single-quotes. From the PHP Manual:
The most important feature of double-quoted strings is the fact that
variable names will be expanded
Try this:
include "$location/index.php"; ?>
Or this:
include $location.'/index.php'; ?>
Either of those should work better. PHP won't actually expand the $variable inside a '' quote, you'll just get $variable as text in the output instead so you want to put the $variable outside the quote using dot between them like the second example or use "" quotes like the first.
you have that a bit backwards...
$location = $row_info['location'].'/index.php';
include $location;
This should work or you could use double-quotes for values and get same results. As stated above. :)~

PHP post is appending extra " \" " Why is it so?

I am using
<textarea id="content" name="content" style="width:0; height:0;">
<?php $content = file_get_contents($url); ?>
</textarea>
and i am posting this text area to a php file
$file = $_POST["content"];
echo $file;
The output that i am getting displayed Everything with an extra \"
All the images , all the references... Any solution for this ?
For a start, the code in the textarea won't actually output anything, since all it's doing is assigning the contents of $url to the $content variable. Try using echo to output it:
<?php echo file_get_contents($url) ?>
As for the slashes, it sounds like a magic quotes problem. You can easily check this in the course of a script by calling get_magic_quotes_gpc, which will return true if the feature is enabled. The PHP website has some useful information on how to disable it.
Check if you have magic quotes activated.
You can check this, either in your configuration-files, or by viewing the output of php_info(). Here are instructions on how to disable this "feature".

Categories