RETURN values in PHP - php

I have 2 functions in PHP and i want to return a value to one function to another. this is my functions
public function save_payment_log_data($icasl_number, $exam_session) {
$paylog_av = $this->payment_log_exists($icasl_number, $exam_session);
}
function payment_log_exists($icasl_no, $exam_session) {
$this->db->where('icasl_no', $icasl_no);
$this->db->where('exam_session', $exam_session);
$query = $this->db->get('exm_paymentlog');
if ($query->num_rows() > 0) {
$pl = $query->row();
$pay_id = $pl->paylog_id;
return $pay_id;
} else {
return false;
}
}
I want to return $pay_id to the save_payment_log_data() function but in here $pay_id didn't return to that function. I think it's return from the function payment_log_exists()
so how can I return $pay_id to the save_payment_log_data() function

See this example it's working fine:
function save_payment_log_data() {
$paylog_av = payment_log_exists();
# print the return value
echo $paylog_av;
}
function payment_log_exists() {
return "Hello";
}
save_payment_log_data();
You can try to remove public and this form save_payment_log_data(),
and call the save_payment_log_data() function where you want.

your are only assigning a value to the $paylog_av your "save_payment_log_data" function should be:
public function save_payment_log_data($icasl_number, $exam_session) {
$paylog_av = $this->payment_log_exists($icasl_number, $exam_session);
return $paylog_av;
}

Related

Return function results based on condition - PHP

I have the following code
public function create(){
return self::checkQuantity();
return self::checkSameLocation();
return self::someAnotherFunction();
return self::someMoreFunction();
..
..
..
}
public function checkQuantity(){
if(someCondition){
return [foo,bar];
}
}
public function checkSameLocation(){
if(someCondition){
return [foo,bar];
}
}
I would like to return function inside create() only if it function inside it returns something else continue executing the create() function.
Based on the example:
create() gets called
if checkQuantity returns nothing then continue with checkSameLocation() without returning checkQuantity() function
NOTE: There could be multiple function calls inside create() sometimes, I would like to avoid using if statement checks.
public function create(){
return self::checkQuantity()
?? self::checkSameLocation()
?? self::someAnotherFunction(); //and so on
}
See Null coalescing operator
public function create(){
$data = function1();
if ($data) {
return $data;
}
$data = function2();
if ($data) {
return $data;
}
$data = function3();
if ($data) {
return $data;
}
// ...
return function42();
}
This can lead to another solution like:
public function create(){
$functions = ['func1', 'func2', 'func3',]; // etc
foreach ($functions as $func) {
$data = $func();
if ($data) {
return $data;
}
}
}
Something like this:
(in this case, I've moved all your check code into polymorphic classes; the same could be achieved with methods in the current class per your question, but I think it's neater this way)
$checks = [new checkClass1, new checkClass2, new checkClass3, ...];
foreach ($checks as $check) {
$result = $check->runCheck();
if ($result) {
return $result;
}
}
You need an if and a temporary variable.
public function create(){
$a = self::checkQuantity();
if(empty($a)) {
return self::checkSameLocation();
} else {
return $a;
}
}
public function checkQuantity(){
if(someCondition){
return [foo,bar];
}
}
public function checkSameLocation(){
if(someCondition){
return [foo,bar];
}
}
This looks like the perfect use case for the null coalescing operator
public function create(){
return self::checkQuantity() ?? self::checkSameLocation() ?? self::someAnotherFunction() ?? return self::someMoreFunction()...
}
You can chain as many function calls as you want. That line will return the first value that's not null. Since returning nothing from a function effectively equals returning a null value, it'll work as you want.

CODEIGNITER function can echo but can't return

I have this code in my model file
function exist_kode($kode = 1)
{
$get = $this->db->query("SELECT * FROM perkiraan WHERE kode_perk='$kode'");
if($get->num_rows() == 0)
{
return $kode;
}
else
{
$this->exist_kode((int)$kode+1);
}
}
This function is not return some values when I use. But if I change return to echo, it will write the value I hope.
How can i solve this?
Return in your else statement as well:
return $this->exist_kode((int)$kode+1);

Not getting desired output from PHP class

This is my very first question actually. I am under a learning process of OOP PHP and trying to design my first class for Category. Here is my code
<?php
class category{
public $catid;
public $datacol;
public function __construct($catid=0)
{
$this->catid=0;
$this->datacol=$this->load($catid);
}
public function load($catid)
{
global $conn;
$sql=' select * '
.' FROM tbl_categories'
.' WHERE catid='.$catid;
if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0)
return false;
$this->datacol = mysqli_fetch_array($res);
$this->catid = $this->datacol['catid'];
return true;
}
public function reload() {
return $this->load($this->getId());
}
/* ------------------> Getter methods <--------------------- */
public function getId() { return $this->catid; }
public function getName() { return $this->datacol['category']; }
public function getOneliner() { return $this->datacol['categoryoneliner']; }
public function getBrief() { return $this->datacol['categorybrief']; }
public function getThumb() { return $this->datacol['timg']; }
public function getImage() { return $this->datacol['limg']; }
public function getParentID() { return $this->datacol['parentcat']; }
public function getPagetitle() { return $this->datacol['catpagetitle']; }
public function getKeywords() { return $this->datacol['catkeywords']; }
public function getMetadesc() { return $this->datacol['categorymetadesc']; }
public function getPriority() { return $this->datacol['priority']; }
public function getRemarks() { return ($this->datacol['remarks']); }
public function getRow() { return $this->datacol; }
}
?>
Now,
When i create its object from other file with a code as below:
$cat=new category(10);
echo var_dump($cat);
echo var_dump($cat->getId());
echo var_dump($cat->getName());
The output gives me the correct catid when called getID(). But Shows NULL for getName() or any other function other than getID().
What am i doing wrong?
Thanks in advance
As #Arnold Gandarillas said, you are overwriting $datacol in the constructor.
Your constructor calls load(), which sets $datacol:
public function load($catid)
{
...
$this->datacol = mysqli_fetch_array($res);
return true;
}
The function then returns true. In the constructor, this true is assigned to $datacol, overwriting the previous value:
public function __construct($catid=0)
{
$this->catid=0;
$this->datacol=$this->load($catid);
}
Change the constructor to something like this:
public function __construct($catid=0)
{
if ($catid > 0)
$this->load($catid);
}
Alright - look closely to your function:
public function load($catid)
{
global $conn;
$sql=' select * '
.' FROM tbl_categories'
.' WHERE catid='.$catid;
if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0)
return false;
$this->datacol = mysqli_fetch_array($res);
$this->catid = $this->datacol['catid'];
return true;
}
So, you function returns either true of false. And this boolean values is assgined to $this->datacol here:
$this->datacol = $this->load($catid);
As this assignment happens after you did $this->datacol = mysqli_fetch_array($res);, your $this->datacol becomes either true or false, and holds no real data from mysql.
So, one of the solutions can be to remove assignment:
public function __construct($catid=0)
{
$this->catid=0;
$this->load($catid);
}
In this case your $this->datacol is not overwritten.
its simple $this->datacol = $this->load($catid); is not required
you have assigning $this->datacol twice
public function __construct($catid=0)
{
$this->catid=0;
$this->load($catid);
}
public function load($catid)
{
global $conn;
$sql=' select * '
.' FROM tbl_categories'
.' WHERE catid='.$catid;
if (!($res=mysqli_query($conn,$sql)) || !mysqli_num_rows($res)>0)
return false;
$this->datacol = mysqli_fetch_array($res);
$this->catid = $this->datacol['catid'];
return true;
}
please add if anything is missing

Get data return php oop

getclass.php
public $set;
//dont need to care here the code is right , here is where it final result
default;
$this->actual_device = "desktop";
echo $this->issetValueNull($this->actual_device);
public function issetValueNull($mixed)
{
$this->set = $mixed;
}
getdata.php
require_once "getclass.php";
$check_detect_device = new detect_device();
if($check_detect_device->issetValueNull->set1 = "desktop"){
"<script>console.log('desktop');</script>";
}
i need to get the data from getclass.php to getdata.php and check the final result at getdata.php , some like below;
//this data return from getclass.php
if(isset($_GET['desktop'])){
"<script>console.log('desktop');</script>";
}
but i dont know how to return the data from getclass , can any one give me some advice ?
Im not sure is this what you are trying to do, but check my example:
<?php
class getClass
{
public $actualDevice;
public function __construct()
{
$this->actualDevice = "desktop";
}
public function setActualDevice($actualDevice)
{
$this->actualDevice = $actualDevice;
return $this;
}
public function getActualDevice()
{
return $this->actualDevice;
}
public function issetActualDevice()
{
return isset($this->actualDevice);
}
}
class getData
{
public $getClass;
public function __construct(getClass $getClass)
{
$this->getClass = $getClass;
}
public function checkDetectDevice($device)
{
if ($this->getClass->getActualDevice() == $device) {
return true;
}
return false;
}
}
$getClass = new getClass();
$getData = new getData($getClass);
if ($getData->checkDetectDevice($_GET['desktop'])) {
echo "<script>console.log('desktop');</script>";
}

Manipulating passed arguments in php

Im not so experienced in php , Im using codeigniter to write my application , I have my own library and within my library there are three functions/methods that passes there arguments in one function which is in one of my models , my question is how will i manipulate/trick the method in my model to know exactly which function among the three in my library has passed the value and return the correct value ..
1st function
public function id_exist ($id) {
if(empty($id)) {
return FALSE;
}
return $this->library_model->this_exist($id);
}
2nd function
public function group_exist($group) {
if(empty($group)){
return FALSE;
}
return $this->library_model->this_exist($group);
}
with the 3rd same as the above 2
in my model
public function this_exist ($item) {
if(empty($item) || !isset($item)) {
return FALSE;
}
// here is where i need to know which function has passed the argument so that i can work with it and return the correct value from the database
}
Might be dirty, might be not sophisticated, but why not passing another argument which tells exactly the origin?
public function id_exist ($id) {
if(empty($id)) {
return FALSE;
}
return $this->library_model->this_exist('id', $id);
}
public function group_exist($group) {
if(empty($group)){
return FALSE;
}
return $this->library_model->this_exist('group', $group);
}
Model:
public function this_exist ($origin, $item) {
if(empty($item) || !isset($item)) {
return FALSE;
}
if($origin == 'id'){
// do something
}
elseif($origin == 'group') {
// do something else
}
}

Categories