Proper way to declare and use a function in php [duplicate] - php

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I have 2 pages
function.php
function getcookie()
{
if (!empty($_COOKIE['remember'])) {
$test="testing function";
}
}
And home.php
include('function.php');
getcookie();
echo"$test";
I get this error - Notice: Undefined variable: test in .... when I call the getcookie function and meanwhile the cookie is set.
When I try it like this, it works.
home.php
if (!empty($_COOKIE['remember'])) {
$test="testing function";
}
echo"$test";
Result - testing functon

if you want to echo from inside a function, you can do it like this as one example. Although there are many ways to do this.
function.php
function getcookie(){
if (!empty($_COOKIE['remember'])) {
echo "testing function"; // Echo this string
}
}
Rest of your code
include('function.php');
getcookie(); // This will output "testing function" from inside your function if your condition is met

Related

variable is undefined PHP [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 3 years ago.
Could please help me understand what I am doing wrong here.
I am able to get the value of $income, but the issue is in the function ovIncome i get this error $income is undefined.
$income = $_REQUEST['income'];
//Gross Income Overview
function ovIncome() {
//Check if Less Than or More Than
if ($income == '0') {
$wageVal = 'Less than € 30.984,- ';
}
elseif($income == '1') {
$wageVal = 'More than € 30.984,- and same as € 61.200,-';
}
else {
$wageVal = 'More than € 30.984,-';
}
echo "$wageVal";
}
You need to pass variable in your function as a parameter.
$income = $_REQUEST['income'];
function ovIncome($income){//pass variable as parameter
And you will good to go.
Sample example: https://3v4l.org/dGYAj
Note: at the time of calling the function you need to pass that variable too( what I did in my code link in last line)

PHP use variable inside function [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 5 years ago.
This is really simple but I can't get my head around it. I am setting a datestamp and would like to be able to use it inside a function like this..
$date_stamp = date("dmy",time());
function myfunction() {
echo $date_stamp;
}
This is not working and $date_stamp is not available inside the function, how can I use this?
This is basic PHP. $date_stamp is out of scope within your function. To be in scope you must pass it as a parameter:
$date_stamp = date("dmy",time());
function myfunction($date) {
echo $date;
}
// call function
myfunction($date_stamp);
See PHP variable scope.
Just as an add-on to John Conde's answer, you can also use a closure like so
<?php
$date_stamp = date("dmy",time());
$myfunction = function() use ($date_stamp) {
echo '$myfunction: date is '. $date_stamp;
};
$myfunction();

How to use a PHP function to write to an array [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
I am trying to write a PHP function to write error messages to an array. Not sure what I'm doing wrong, still trying to get to grips with functions.
I can make it work without functions, so I guess its the way I'm writing the function that is wrong.
function writeerrors($arr_key, $arr_val){
$errors[$arr_key] = $arr_val;
return;
}
Then I call it here when I check if the form field is empty. If it is empty I want it to write to the $errors array.
//check if empty
if(empty($fname)){
//write to error array
writeerrors('fname', 'Empty field - error');
//Flag
$errors_detected = true;
}else {
Do something else ..}
This is the form... (ONLY TRYING TO VALIDATE FIRST NAME FIELD FOR NOW):
http://titan.dcs.bbk.ac.uk/~mgreen21/p1_prac/PHP_BBK/P1/hoe9/index.php
You just need to specify the global $errors variable, which you have created outside of your function.
function writeerrors($arr_key, $arr_val){
global $errors;
$errors[$arr_key] = $arr_val;
return;
}

Accessing variable inside function - php [duplicate]

This question already has answers here:
Can't access global variable inside function
(5 answers)
Closed 9 years ago.
i need to access a variable which is declared in another php file within a function.. How can i do it?
a.php
<?php
$global['words']=array("one","two","three");
echo "welcome"
?>
b.php
<?php
$words = $global['words'];
require_once('a.php');
print_r($global['words']);
function fun()
{
print_r($global['words']); // not displaying here
}
fun();
?>
now i am able to access the "$global['words']" variable in b.php file, but not within function, how can i make it visible inside the function?
The preferred option is to pass as a parameter:
function fun($local) {
print_r($local['words']);
}
fun($global);
If for some reason you can't use that approach, then you can declare the variable as global:
function fun() {
global $global;
print_r($global['words']);
}
fun();
Or use the $GLOBALS array:
function fun() {
print_r($GLOBALS['global']['words']);
}
fun();
But in general, using global variables is considered bad practise.
actually your function does n't know about anything outside it, if it's not a classes, or global php vars such as $_POST , you can try to define function as:
function fun() use ($globals)
{
}

How to echo a property of an object defined in another file [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 9 years ago.
I'm starting to learn PHP and OOP and I'm stuck. I have 3 different php
files that I paste below. The particular problem is stated after the code:
File1.php :
<?php
class Page{
public $intro;
public $article;
}
$TD = new Page($intro, $article);
$TD->intro="I'm the intro";
$TD->article="I'm an article";
?>
File2.php
<?php
function test($page){
switch($page){
case "A":
include "file1.php";
break;
case "B":
include "anotherfile.php";
break;
}
}
?>
File3.php (the one that has to print something):
<?php
$page="A";
include "file2.php";
test($page);
echo $TD->intro;
echo $TD->article;
?>
I can't echoing (says that $TD is undefined), but I've been testing and it seems that it's effectively loading the file1.php (where the $TD object is defined). Furthermore, if I paste the problematic echoes in the file1.php and loading this page, the echoes work.
I suppose that it's something obvious but I am not capable of figure it out yet.
Thanks in advance for your response and for reading this to the end!!! :)
Because your include is in the scope of the function, so the variables that are defined in the include are only visible inside of the function.
function test($page){
switch( $page) {
case "A":
include "file1.php";
// $TD is in scope here, but not outside this scope
break;
}
}
A quick fix is to add global $TD; at the top of your test() function.

Categories