Why aren't my variables printing? 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 5 years ago.
Could anyone tell me why my character stats will not print? I'm not getting any error or syntax messages..
This is my first php project ever. I can't figure out what I'm missing!
<?
class character{
public $healthpoints = 100;
public $isdead = false;
public $class = "Mage";
public $level = 10;
}
function checkdeath() {
if($healthpoints >= 0){
$isdead = true;
}
}
new character();
function CharStats() {
echo $healthpoints;
echo $isdead;
echo $class;
echo $level;
}
CharStats;
?>

I can't figure out what I'm missing!
Thinking, ALL
class Character{
public $healthpoints = 100;
public $isdead = false;
public $class = "Mage";
public $level = 10;
function checkdeath() {
if($this->healthpoints >= 0){
$this->isdead = true;
}
}
function CharStats() {
echo $this->healthpoints;
echo $this->$isdead;
echo $this->$class;
echo $this->$level;
}
}
$character = new Character();
$character->ChatStats();
Read again about classes/objects/etc. - Classes and Objects in PHP

Related

Linked list in php giving errors

Im new to PHP and I'm trying to make a linked list but it keeps on giving errors
<?php
class Node {
private $value;
private $nxt;
function __construct($x) {
$this->value = $x;
$this->set_nxt(null);
}
function set_value($x) {
$this->value = $x;
}
function get_value() {
return $this->value;
}
function set_next($x) {
$this->nxt = $x;
}
function get_next() {
return $this->nxt;
}
}
class linked_list {
private $start = new Node(null);//error is here
function __construct() {
$start = new Node(null);
}
function add_name($nme) {
$start = new Node($nme);
if ($start->get_value() == null) {
$start = new Node(nme);
$start->set_next(null);
} else {
$temp = new Node($nme);
$temp->set_next($start);
$start = $temp;
}
}
function show_all() {
$temp = $start;
while ($temp != null) {
echo $temp->get_value();
echo "<br/>";
$temp = $temp->get_next();
}
}
}
?>
It would be great if you could tell me what I am doing wrong and how I should do it right. Please I just want to know what I am doing wrong with the PHP code. There is no need to tell me about linked list I just want to know what I am doing wrong with the implementation.
Here is the data I am working with :
$list = new linked_list();
$list->add_name("first");
$list->add_name("second");
$list->add_name("third");
$list->add_name("fourth");
$list->show_all();
and here is the error :
Basically its saying the $start in linked list class is a constant. i have commented on the place the error is coming from
You have at least four errors to solve:
$this->set_nxt(null); in the constructor of the Node class: set_nxt is an undefined function (maybe you meant set_next);
private $start = new Node(null); in the linked_list class: you can't declare a property and initialise it with a new instance of a class (you can do it inside the constructor);
$start = new Node(nme); in the add_name function of the linked_list class: nme is an undefined constant (maybe you meant $name);
$temp = $start; in the show_all function of the the linked_list class: $start is an undefined variable (maybe you meant $this->start).

is it possible to call the multiple methods in single object like this? [duplicate]

This question already has answers here:
PHP method chaining or fluent interface?
(10 answers)
Closed 2 years ago.
I tried calling multiple functions on a single object. But I got this error
Uncaught Error: Call to a member function dordor()
Could you correct me please?
<?php
class Das
{
public $a= 'mulut';
public $b = 'anda';
public $c = 'kotor';
public function dor(){
echo $this->a.PHP_EOL;
echo $this->b.PHP_EOL;
echo $this->c.PHP_EOL;
echo PHP_EOL;
}
public function dordor(){
echo 'lmao';
echo PHP_EOL;
}
}
$s = new Das();
$s->a = 'mulut';
$s->b = 'anda';
$s->c = 'kotor';
$s
->dor()
->dordor();
?>
What you want to implement, if I have understood you correctly, is often called "chaining".
To implement this you have to return the object itself, i.e. "this", at the end of your method.
I have adapted your code accordingly
<?php
class Das
{
public $a= 'mulut';
public $b = 'anda';
public $c = 'kotor';
public function dor(){
echo $this->a.PHP_EOL;
echo $this->b.PHP_EOL;
echo $this->c.PHP_EOL;
echo PHP_EOL;
return $this;
}
public function dordor(){
echo 'lmao';
echo PHP_EOL;
return $this;
}
}
$s = new Das();
$s->a = 'mulut';
$s->b = 'anda';
$s->c = 'kotor';
$s
->dor()
->dordor();
?>
You are using a method as an object. $s->dor()->dordor();
But dor() is not an object and it is a method.
you have to add $this->dordor(); at the end of your dor() method codes which is call dordor() method with same object that you called dor() method.
This:
<?php
class Das
{
public $a= 'mulut';
public $b = 'anda';
public $c = 'kotor';
public function dor(){
echo $this->a.PHP_EOL;
echo $this->b.PHP_EOL;
echo $this->c.PHP_EOL;
echo PHP_EOL;
$this->dordor();
}
public function dordor(){
echo 'lmao';
echo PHP_EOL;
}
}
$s = new Das();
$s->a = 'mulut';
$s->b = 'anda';
$s->c = 'kotor';
$s->dor();
?>

call variabel from other function (php) [duplicate]

This question already has answers here:
Access variable from scope of another function?
(3 answers)
Closed 5 years ago.
I have this code
class foo
{
function one()
{
$number = 1;
}
function two()
{
echo $number;
}
}
And i want to call $number from function one() on function two().
Is it possible to do this ?
Hello Mfdsix Indo,
Try this code,
class foo
{
function one()
{
$number = 1;
return $number;
}
function two()
{
echo $this->one();
}
}
OR
class foo
{
private $number;
function one()
{
$this->number = 1;
}
function two()
{
echo $this->number;
}
}
I hope my answer id helpful.
If any query so comment please.

Trying to stack values with PHP

Im currently trying to make a little dice game in php. Right now Im trying to make a "currentscore" where all the points from a rand(1,6) are stacked into a single variable.
Here is the class Im doing this in:
<?php
class CDice {
public $roll;
public $currentscore;
public function Roll()
{
$this->roll = rand(1,6);
return $this->roll;
}
public function currentScore()
{
$this->currentscore += $this->roll;
return $this->currentscore;
}
}
I don't under stand why $this->currentscore += $this->roll; doesn't work.
You do realise that at the end of execution of this class no values are kept right? If you wish to transfer over this data to the next page render, you should use PHP sessions.
<?php
class CDice {
public $roll;
public $currentscore = 0;
public function Roll(){
$this->roll = rand(1,6);
$this->currentscore += $this->roll;
return $this->roll;
}
public function currentScore(){
return $this->currentscore;
}
public function __construct(){
if(session_status() == PHP_SESSION_ACTIVE){
$this->currentscore = isset($_SESSION['dice.score']) ? $_SESSION['dice.score'] ? 0;
# In PHP 7.0
# $this->currentscore = $_SESSION['dice.score'] ?? 0;
} else {
echo 'session has not been initiated';
}
}
}
session_start();
$tmp = new CDice();
echo $tmp->Roll();
echo $tmp->Roll();
echo $tmp->currentScore();
?>
Also not assigning an "initalial" value to a variable before trying to add things to it with +=, -=, etc causes PHP to throw a warning.

How to create objects in PHP [duplicate]

This question already has answers here:
How to define an empty object in PHP
(17 answers)
Closed 10 years ago.
I'm having difficulty understanding how to create objects in my script.... i get this error :
PHP Fatal error: Call to undefined function Object()
My code is like this:
$block = Object(); // error here
$row['x'] = 5;
$row['y'] = 7;
$row['widthx'] = 3;
$row['widthy'] = 3;
for($i = $row['x']; $i < ($row['x'] + $row['widthx']); $i++){
if(!is_object($block[$i])){
$block[$i] = Object();
}
}
Can some one explain what i'm doing incorrectly?
In the simplest form, objects are classes.
class coOrds {
// create a store for coordinates
private $xy;
function __contruct() {
// it's still an array in the end
$this->xy = array();
}
function checkXY($x, $y) {
// check if xy exists
return isset($this->xy[$x][$y]);
}
function saveXY($x, $y) {
// check if XY exists
if ($this->checkXY) {
// it already exists
return false;
} else {
// save it
if (!isset($this->xy[$x])) {
// create x if it doesn't already exist
$this->xy[$x] = array();
}
// create y
$this->xy[$x][$y] = '';
// return
return true;
}
}
}
$coords = new coOrds();
$coords->saveXY(4, 5); // true
$coords->saveXY(5, 5); // true
$coords->saveXY(4, 5); // false, already exists
Start reading about them here: http://www.php.net/manual/en/language.oop5.basic.php
You need to define classes and instance them as objects:
class Object {
private $name;
__construct($name){
$this->name=$name
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
}
$block = $new Object($name);

Categories