I'm curious as to why variables aren't echoing using a function... (If that makes sense lol..)
function name(){
echo "$info->fullname";
}
When i then use
<?php name(); ?>
anywhere on withing the script, it's completely blank, yet if i remove the variable and put static text, it echo's out just fine...
It works fine without using it as a function, it echo's "Joe Bloggs"...
Not quite sure why it doesn't work ? lol
Any ideas guys?
yes MarkBaker is right. and you should use global. something like this.
function name(){
global $info;
echo $info->fullname;
}
or pass the variable to function like this.
function name($info){
global $info;
echo $info->fullname;
}
use
<?php name($info); ?>
and one advice which is not important at all : " is slower than ' try to use " only if needed.
Related
So basically I'm trying to include files in an independent(from where I include it) way and I'm getting a hard time figuring how I really should to it in terms of good architecture etc.
I've tried something like this:
public_html/functions/functions.php:
function load_header() {
include( get_path() . 'load/header.php' );
}
function get_path() {
return 'https://example.com/';
}
public_html/index.php:
include('functions/functions.php');
$path = get_path();
load_header();
public_html/load/header.php:
// code
// using variable $path inside of it
// code
The problem with the above code is... everything, really. I can't get access to the $path and what not.
Is there some well-known-way to handle something like this? I'm sure there must be something... I don't want to use any frameworks or anything like that.
Hope for your help, Thanks
You should pass it as parameters. Had you done this without the functions, there would be no problems, e.g.
# file1.php
$test = 'hello, world';
# file2.php
include 'file1.php';
echo $test;
works, but due to function scope, you don't have access to the $test variable.
Pass it to the function via parameters:
function load_header($path)
{
include(get_path(). 'load/header.php');
}
now inside header.php you have $path.
Is it bad practice to echo out a bunch of HTML using a function in php and having it something like this:
function my_function() {
global $post;
$custom_fields = get_post_custom();
$some_field = $custom_fields ['some_field'][0];
?>
<div class="something <?php if ($some_field) { echo $special-clas;} ?>">
<div class="something-else">
/* bunch more of html code */
</div>
</div>
}
And then in the page where you want to use that to echo it?
<html>
<body>
.....
....
<?php echo my_function(); ?>
....
I'm unsure how "accepted" it is to echo out functions?
Consider two functions:
function does_return() {
return 'foo';
}
function does_echo() {
echo 'bar';
}
does_return(); // nothing displayed
echo does_return(); // 'foo' displayed
does_echo(); // 'bar' displayed
echo does_echo(); // 'bar' displayed
In both cases, output CAN be performed, but how it happens differs. Since does_return() does not itself have ANY code to perform output within its definition, output is up to the calling code, e.g. the echo you perform.
With does_echo(), it doesn't matter how you call the function (with or without echo), since the function does the output itself. you'll get bar regardless.
Now consider this:
function this_is_fun();
echo 'foo';
return 'bar';
}
this_is_fun(); // outputs 'foo'
echo this_is_fun(); // outputs 'foobar';
This is bad practice, because it makes your code hard to maintain.
With a function like that you are mixing the logic and presentation. So, when you see something in your output that you don't like you can not be sure where to go first to go and change it. Do you go to the page code or the function code?
Functions are supposed to return data, and then your application deals with it how you wish, whether that’s assigning it to a variable or echoing it out.
I don't see how it's bad practice. As long as you're reusing the function, then it seems like you're using it the right way.
The only thing you shouldn't be doing is using global; rather pass $post to the function. See this answer for why.
Since your function already has an output, you don't need the echo.
my_function( $post );
That's fine. I'd rather see that than the PHP mixed in completely to the HTML.
You can use <?= my_function() ?> instead if you want to write a little less code.
What #DaveRandom said in his comment. Aside from that, no, it's not necessarily bad practice. It can though make for code that's hard to debug. Consider a MVC approach instead where the logic is largely in the Controller and the View simply handles rendering the view based on that logic.
I'm trying to use PHP variables into echoed file, and couldn't get where is a trouble at first using that script:
$head = new mod_head("head.php");
$id="ASDSSgdfsfsdfS";
echo $head;
mod_head class:
class mod_head
{
private $out="";
function __construct($arg)
{
$this->out=$this->parts($arg);
}
public function __toString()
{
return $this->out;
}
private function parts($file)
{
return fread(#fopen(PATH . "parts/".$file, 'r'), filesize(PATH . "parts/".$file));
}
}
and the file is "head.php"
<h1><center style="background:orange; border-radius:15px;">LOGO</center></h1>
<br><?php print_r($id)?>
<div>BANNER <div>$id <?php echo $id ;?></div></div>
i dont want to create global vars, why it doest echo $id var?
First, you're going to need to parse the file, not just read it. The second problem you'll have is a scope issue. $id is outside of the scope of the parts() function. In order to return the contents of the required file instead of just printing it I've used the output control functions
Try changing your parts function to this:
private function parts($file)
{
ob_start();
require(PATH . "parts/".$file);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
To fix the scope issue try changing $id="ASDSSgdfsfsdfS"; to $head->id = "ASDSSgdfsfsdfS";, then change head.php to be the following:
<h1><center style="background:orange; border-radius:15px;">LOGO</center></h1>
<br><?php print_r($this->id)?>
<div>BANNER <div>$id <?php echo $this->id ;?></div></div>
Simply reading a file with fread will not parse any PHP contained inside. Perhaps you are looking for something like:
http://php.net/manual/en/function.require.php
Using require() is basically like copying and pasting the required file directly where your require() statement is. This means that the required file would only be able to use variables that are within the scope of where the require() statement is.
Because you are reading the contents of head.php and echoing them verbatim; nowhere do you make PHP compile and run that file as code. You could do that using include('head.php'), but that would not work blindly because you also have to make sure that $id is in scope at the point you do the include.
However that's not as easy as it sounds because it is not possible to automatically "pack the whole local scope" for later use because automatically implies code inside a function, and the very act of calling that function causes the scope to change.
My thought is that you are probably using mod_head is passing in a php file so it can be used as a view, right?
if thats the case, usually there is also a way to include a variable so that way it exists in the other file, head.php.. since there isn't one, you'd need to create a method for that. and then make that fread an include or require instead.
If not, and you're simply loading the file, the other comments about fread not parsing is totally correct and you will not be able to simply access $id from the other file.
How do I set a piece of code to a function in WordPress and then call that function...this was my first guess, but of course this doesn't work...
Could someone suggest how I can get this to work so I can avoid redundant code. Also if I define code in a function can I call it in different .php files, or can I only call it within that file?
<?php
// This code will never change.
function $test {
echo('test this will be a long string and repeated many times.')
}
?>
<?php
echo $test;
?>
PHP function names (unlike variables) are declared without dollar signs:
function test (
To call a function, use this format:
test();
Your code example will look like this:
<?php
// This code will never change.
function test {
echo('test this will be a long string and repeated many times.')
}
?>
<?php
test();
?>
I face a strange problem including php files. Let me show you the code:
// constants.php
$MYSQL_HOST_PORT = 'localhost:3306';
// functions.php
include 'constants.php';
function getVar() {
echo $MYSQL_HOST_PORT;
}
// doSth.php
include 'functions.php';
echo $MYSQL_HOST_PORT; // The variable is visible and echoed normally as expected!
echo getVar(); // The variable is not echoed! its "".
Any ideas ?
For one, the echo in echo getVar(); won't ever print anything, because getVar doesn't return a value.
Secondly, if you (for some reason) want getVar() itself to work correctly, you need to add a global $MYSQL_HOST_PORT; line, to make it look for $MYSQL_HOST_PORT in the global scope.
Rather than globalising the $MYSQL_HOST_PORT variable, why not simply make it a constant?
// constants.php
define('MYSQL_HOST_PORT', 'localhost:3306');
Provided constants.php is included, you can reference the MYSQL_HOST_PORT constant anywhere.
As indicated in zerocrate's answer, the issue is a scoping one. The enclosed scope of the getVar() function does not include $MYSQL_HOST_PORT.
One thing that I can see wrong is that with the line echo getVar(); you are not getting a return value from the function so you can simply write getVar(); by itself.