-1

I am bringing some info trough $_POST, but when I try to make use of this info into any function, console emerges an error about Undefined Variable (in this example, $ms_img brings an error, as well as $con -the connection query declared on conexionbbdd.php- and any othe variable inside functions.

EDIT: Neither passing as arguments works, it emerges one error per argument as this:

Warning: Missing argument 1 for checkCost(), called in C:\wamp\www...\actions\msg_newMessage.php on line 96 and defined in C:\wamp\www...\actions\msg_newMessage.php on line 17

function checkCost($con,$ms_img,$id){...}

CODE:

<?php
 session_start();
 include("../conexionbbdd.php");
 include("../conexionapi.php");
 $id = $_SESSION['id']; 
 $inclass = $_SESSION['inclass']; 
if($_SESSION['estado'] == 'activo'){
 $ms_content = $_POST['ms_content'];
 $ms_img = $_POST['ms_img'];
 $ms_prefix = $_POST['ms_prefix'];
 $ms_phone = $_POST['ms_phone'];
 function checkCost(){
 $totalCost = 0;
 if ($ms_img!=""){
 $totalCost=2;
 } 
 else{
 $totalCost=1;
 }
 $checkCredits=mysqli_query($con,"SELECT us_credits FROM ws_users WHERE us_id=$id");
 }
 function sendMessage(){
 //WHATEVER
 }
 if($inclass==='1'){ 
 checkCost(); 
 }
 else{
 sendMessage(); 
 }
}else{
header('location:../login.php');
}
?>
Magicprog.fr
4,1204 gold badges28 silver badges35 bronze badges
asked Sep 11, 2015 at 10:36
3
  • You need to pass the variables as arguments to the function or declare them with global. Commented Sep 11, 2015 at 10:48
  • Do not use glob to inject global vars into a funciton. In long term this can result very confusing code. Commented Sep 11, 2015 at 10:50
  • possible duplicate of PHP: variable not working inside of function? Commented Sep 11, 2015 at 11:28

1 Answer 1

0

You can have access to superglobal variables like $_POST in any function. But global variables such as $con that should be initialized in an including file is not accessable in a function. You can pass it as parameter. And do not use the global keyword to inject global vars into a funciton. In long term this can result very confusing code. So you could do:

function checkCost($con){
 $ms_content = $_POST['ms_content'];
 $ms_img = $_POST['ms_img'];
 $ms_prefix = $_POST['ms_prefix'];
 $ms_phone = $_POST['ms_phone'];
 $totalCost = 0;
 if ($ms_img!=""){
 $totalCost=2;
 } 
 else{
 $totalCost=1;
 }
 $checkCredits=mysqli_query($con,"SELECT us_credits FROM ws_users WHERE us_id=$id");
 }

But its still very procedural. I recommend you to start to read OOP.

answered Sep 11, 2015 at 10:48
1
  • I do need OOP, that's for sure. But it is still a bit confusing for me. Thank you for your explanation, worked. Commented Sep 11, 2015 at 10:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.